我的目标是拆分数据框,在每个组上运行igraph的graph_from_data_frame,并以某种方式将其合并回原始数据框。
到目前为止,我已经能够获得igraph函数返回我认为是图形参数的列表,但我无法分辨,因为我无法在列出的行内“看到”。这是一些可复制的代码:
set.seed(123)
Data <- data.frame(
From = sample(c("Dan", "Sharon","Bob","Andrew"), 100, replace = TRUE),
To = sample(c("Dan", "Sharon","Bob","Andrew"), 100, replace = TRUE),
Time=sample(c(1,2,3),100, replace = TRUE),
ID.match=1:100)
Data %>% View
我想拉出图表的中心度量,并将它们与ID.match变量结合起来。然后,我计划对已包含在我的数据集中的其他感兴趣的变量回归这些度量。我正在使用group_by on Time为每个时间点创建一个图表,如下所示:
Data %>% group_by(Time) %>% do(v=graph_from_data_frame(.))
igraph函数graph_from_data_frame创建一个igraph对象,从中可以获得中心性度量。以下代码可以执行我想要的 without group_by 。我想在group_by中使用它:
set.seed(123)
Data <- data.frame(
From = sample(c("Dan", "Sharon","Bob","Andrew"), 100, replace = TRUE),
To = sample(c("Dan", "Sharon","Bob","Andrew"), 100, replace = TRUE),
# Time=sample(c(1,2,3),100, replace = TRUE),
ID.match=1:100)
Data %>% View
g <- graph_from_data_frame(Data)
plot(g)
metrics <- data.frame(
Degree=degree(g),
Closeness = closeness(g),
Betweenness = betweenness(g)
)
metrics %>% View
我希望每个组都有一个“指标”数据框 。这个问题类似于SO question,但我似乎无法解决问题。我试图使用purrr包来取消列出列出的数据帧,但我认为这对我来说有点太高级了。任何帮助将不胜感激。
答案 0 :(得分:1)
DF <- data.frame(
From = sample(c("Dan", "Sharon","Bob","Andrew"), 100, replace = TRUE),
To = sample(c("Dan", "Sharon","Bob","Andrew"), 100, replace = TRUE),
Time=sample(c(1,2,3),100, replace = TRUE),
ID.match=1:100)
制作指标数据框的功能
makemetrics <- function(gr) {
data.frame(Degree=degree(gr), Closeness = closeness(gr), Betweenness = betweenness(gr))
}
Dsplit <- split(DF, DF$Time)
lapply(Dsplit, function(x) makemetrics(graph_from_data_frame(x)))
$`1`
Degree Closeness Betweenness
Andrew 17 0.3333333 0.1818182
Bob 8 0.2000000 0.0000000
Sharon 11 0.2500000 2.0000000
Dan 20 0.3333333 0.8181818
$`2`
Degree Closeness Betweenness
Andrew 17 0.2500000 0.0000000
Dan 19 0.3333333 0.0000000
Bob 17 0.3333333 0.6666667
Sharon 19 0.3333333 0.3333333
$`3`
Degree Closeness Betweenness
Sharon 26 0.3333333 0.8
Bob 17 0.3333333 0.0
Dan 15 0.3333333 0.2
Andrew 14 0.2500000 0.0
您可以使用purrr:map_df
ans <- lapply(Dsplit, function(x) makemetrics(graph_from_data_frame(x)))
library(purrr)
map_df(ans, ~.x, .id="Time")
<强>输出强>
Time Degree Closeness Betweenness
1 1 17 0.3333333 0.1818182
2 1 8 0.2000000 0.0000000
3 1 11 0.2500000 2.0000000
4 1 20 0.3333333 0.8181818
5 2 17 0.2500000 0.0000000
6 2 19 0.3333333 0.0000000
7 2 17 0.3333333 0.6666667
8 2 19 0.3333333 0.3333333
9 3 26 0.3333333 0.8000000
10 3 17 0.3333333 0.0000000
11 3 15 0.3333333 0.2000000
12 3 14 0.2500000 0.0000000