准备在单列中具有多个节点的数据框以用于 igraph
Preparing a data frame with multiple nodes in a single column for use in igraph
我有一个格式大致如下的数据框:
df1 <- data.frame(
x = c(
"Ben, Ellen",
"BV, Ellen, Trev",
"Ben, Trev, Pat",
"Ellen, Ben",
"Ev, Ryan"))
其中每个观察都是一组节点(朋友)。
我想要 igraph
数据,但我假设它首先需要看起来像这样:
df2 <- data.frame(
x = c(
"Ben","BV","BV","Ellen","Ben",
"Ben","Trev","Ellen","Ev"),
y = c(
"Ellen","Ellen","Trev","Trev","Trev",
"Pat","Pat","Ben","Ryan"))
如何对每个观察进行字符串拆分并确保每个节点(朋友)都与观察中的每个其他节点链接?基本上,我怎样才能把 df1
变成 df2
?
使用基函数可能更容易
ss <- strsplit(as.character(df1$x),", ")
el <- do.call("rbind", Map(function(x) t(combn(x,2)), ss))
你的数据 returns
[,1] [,2]
[1,] "Ben" "Ellen"
[2,] "BV" "Ellen"
[3,] "BV" "Trev"
[4,] "Ellen" "Trev"
[5,] "Ben" "Trev"
[6,] "Ben" "Pat"
[7,] "Trev" "Pat"
[8,] "Ellen" "Ben"
[9,] "Ev" "Ryan"
你可以用 igraph
绘制它
plot(graph_from_edgelist(el))
我有一个格式大致如下的数据框:
df1 <- data.frame(
x = c(
"Ben, Ellen",
"BV, Ellen, Trev",
"Ben, Trev, Pat",
"Ellen, Ben",
"Ev, Ryan"))
其中每个观察都是一组节点(朋友)。
我想要 igraph
数据,但我假设它首先需要看起来像这样:
df2 <- data.frame(
x = c(
"Ben","BV","BV","Ellen","Ben",
"Ben","Trev","Ellen","Ev"),
y = c(
"Ellen","Ellen","Trev","Trev","Trev",
"Pat","Pat","Ben","Ryan"))
如何对每个观察进行字符串拆分并确保每个节点(朋友)都与观察中的每个其他节点链接?基本上,我怎样才能把 df1
变成 df2
?
使用基函数可能更容易
ss <- strsplit(as.character(df1$x),", ")
el <- do.call("rbind", Map(function(x) t(combn(x,2)), ss))
你的数据 returns
[,1] [,2]
[1,] "Ben" "Ellen"
[2,] "BV" "Ellen"
[3,] "BV" "Trev"
[4,] "Ellen" "Trev"
[5,] "Ben" "Trev"
[6,] "Ben" "Pat"
[7,] "Trev" "Pat"
[8,] "Ellen" "Ben"
[9,] "Ev" "Ryan"
你可以用 igraph
绘制它plot(graph_from_edgelist(el))