r - 如何在 R 中将数据框添加为具有匹配名称的顶点属性?
问题描述
在 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 表,我需要添加以下属性,即等级和组织。组织单位表
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()
,但我无法弄清楚如何让它通过数据框并且只向现有节点添加属性。例如,在 org.unit 表中 Jane 的成绩是 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 }))
我花了 2 天时间尝试不同的方法,但没有一个有效。请帮忙。
解决方案
您可以像这样gD
从表中设置图形的属性: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
多次将您的 -table 子集化,但它很聪明,因为它保证了顶点的正确顺序。如果你要合并你的表,你的属性顺序会打乱:
# 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”的节点,则代码应处理该问题,但为多个 Janes 分配相同的等级和组织,如 中所示org.unit
。
一切顺利
推荐阅读
- python-3.x - 通过 Python3.7 连接到 SQL Server 的操作错误
- python - 在一个数字序列中找到最大的 n 位乘积
- delphi - Delphi 计算 TPopupMenu 大小
- azure - Azure - GCP - 通过 Openvpn 连接
- batch-file - 任务自动启动时任务调度器权限不足
- orbeon - 登录后,显示 HTTP 状态 403
- json - 如何从 Angular 添加或编辑客户端 json 文件?
- node.js - AWS-EB 错误:找不到模块“快递”
- php - 如何将网站连接到mysql数据库
- arrays - importhtml 每个 URL 中不同数量的列会引发错误并进一步需要移动/随机排列列