在R中,我有如下图“gD”
IGRAPH 40b044a UN-- 17 38 --
+ attr: name (v/c)
+ edges from 40b044a (vertex names):
[1] Jane --Jay Jane --Brian Jane --David Jane --Sarah
[5] Jane --Tom Jay --Christian Jay --David Jay --Sarah
[9] Jay --Dave Jay --Josep Jay --Ray Brian --David
[13] Brian --Sarah Brian --Christin Brian --Tom Christian--Sarah
[17] Christian--Jim Christian--Dave Christian--Josep Michael --David
[21] Michael --Christin Michael --Tim David --Tim David --Tom
[25] David --Dave David --Zemma David --Ray Jim --Josep
[29] Christin --Tom Christin --Zemma Tim --Dickson Tim --Zemma
+ ... omitted several edges
这是org.unit表,我需要添加以下属性,即grade和org。 org.unit表
name grade org
1 Jane 11 HR
2 Tom 11 Finance
3 David 9 Marketing
4 Jay 9 Marketing
5 Brian 8 GTO
6 Christian 7 GTO
7 Tim 5 Commercial Bank
我试图使用set.vertex.attribute()
,但我无法弄清楚如何让它通过数据框并只添加属性到现有节点。例如,Jane的等级在org.unit表中是11。我需要先从图dD中检查Jane,然后从org.unit表中分配相应的Jane等级。
当我在下面尝试时,我收到错误消息“在换行期间出错:3个参数传递给'$',需要2”
gD <- gD %>%set_vertex_attr( .,name = 'grade', index = V(gD), value = sapply(V(gD)$name, function(x){org.unit %>% filter( org.unit$name == x) %>% org.unit$grade }))
我花了两天尝试不同的方式,但没有一个工作。请帮忙。
您可以设置图表gD
from表格tab
的属性,如下所示:
V(gD)$attribute <- sapply(V(gD)$name, function(x) tab$attribute[tab$virtex.name == x])
小工作代码示例:
这应该模仿您的数据结构:
library(igraph)
# Simple example network similar to your data?
org.unit <- data.frame(name =c("Jane", "Tom", "David", "Jay", "Brian", "Christian", "Tim"),
grade=c( 11, 11, 9, 9, 8, 7, 5),
org =c( "HR", "Finance", "Marketing", "Marketing", "GTO", "GTO", "Bank"))
relations <- data.frame(from=c("Jane", "Jane", "Jane", "Jay", "Jay", "David"),
to = c( "Jay", "Brian", "David", "Christian", "David", "Christian"))
# Make a graph from the relations
gD <- graph.data.frame(relations, directed=TRUE)
# Set virtex atributes
V(gD)$grade <- sapply(V(gD)$name, function(x) org.unit$grade[org.unit$name == x])
V(gD)$org.unit <- sapply(V(gD)$name, function(x) as.character(org.unit$org[org.unit$name == x]))
plot(gD)
# Look at it:
V(gD)$grade
说明:
使用sapply()
是愚蠢的,因为它多次将你的org.unit
表子集,但它很聪明,因为它保证了顶点的正确顺序。如果你要合并你的表,你的属性的顺序将加扰:
# Using merge() will scramble your vertex order:
attribute_values <- merge(org.unit, data.frame(name=V(gD)$name), by="name")
(attribute_values$name == V(gD)$name)
您当然可以对它们进行排序以强制它们在图表中显示的顺序:
# If you make an attributes table to set from, it would have to be re-ordered
attribute_values <- attribute_values[ match(attribute_values$name, data.frame(name=V(gD)$name)$name), ]
(attribute_values)
# This is the order of your vertices in the graph
(V(gD)$name)
现在你有一个很好的表,按正确的顺序使用你喜欢的方法设置顶点属性。你使用set_vertex_attr()
。任何这些都可行:
V(gD)$grade <- attribute_values$grade
gD <- set_vertex_attr(gD, 'grade', V(gD), attribute_values$grade)
请注意:
所有这些代码都假设name
是data.frame org.unit
的唯一标识符。如果没有,“简”将有多个等级和组织。确认这返回false:
# Are there duplicates in org.unit?
!length(unique(org.unit$name)) == length(org.unit$name)
如果您有多个名为的节点,例如“Jane”,则代码应处理该节点,但会为org.unit
中指定的多个Janes分配相同的等级和组织。
祝一切顺利