将DF转换为Edge DF以进行协作网络

时间:2019-04-27 14:55:36

标签: r

我有这个df,其中包含有关文章协作的信息:

     author    author2 author3 author4
1      A       D       E       F
2      B       G
3      C       H       F

我需要创建一个edges数据框,其中包含作者之间的关系,如下所示:

    from      to 
1      A       D   
2      A       E
3      A       F  
4      B       G
5      C       H
6      C       F
7      D       E
8      D       F
9      E       F
11     H       F

有什么想法怎么做?

2 个答案:

答案 0 :(得分:1)

我们可以gather对其余列(即该列的左侧)library(tidyverse) map_dfr(names(df)[-length(df)], ~select(df,.x:ncol(df)) %>% gather( k,to,-.x) %>% arrange(!!ensym(.x)) %>% select(-k) %>% filter(to!='') %>% rename(form=starts_with('author'))) form to 1 A D 2 A E 3 A F 4 B G 5 C H 6 C F 7 D E 8 D F 9 H F 10 E F 进行绑定。

df <- structure(list(author = c("A", "B", "C"), author2 = c("D", "G", 
"H"), author3 = c("E", "", "F"), author4 = c("F","", "")), class = "data.frame", row.names = c("1", 
"2", "3"))

数据

from google.colab import drive
drive.mount('/content/drive')

答案 1 :(得分:1)

您可以在函数内逐行应用combn,而无需打包。

edges <- setNames(as.data.frame(do.call(rbind, lapply(seq(nrow(d)), function(x) 
  matrix(unlist(t(combn(na.omit(unlist(d[x, ])), 2))), ncol=2)))), c("from", "to"))
edges
#    from to
# 1     A  D
# 2     A  E
# 3     A  F
# 4     D  E
# 5     D  F
# 6     E  F
# 7     B  G
# 8     C  H
# 9     C  F
# 10    H  F

或者,按照{em> @akrun 的建议使用igraph包。

library(igraph)
edges <- do.call(rbind, apply(d, 1, function(x) 
  as_data_frame(graph_from_data_frame(t(combn(na.omit(x), 2))))))
edges
#    from to
# 1     A  D
# 2     A  E
# 3     A  F
# 4     D  E
# 5     D  F
# 6     E  F
# 7     B  G
# 8     C  H
# 9     C  F
# 10    H  F

数据

d <- structure(list(author = c("A", "B", "C"), author2 = c("D", "G", 
"H"), author3 = c("E", NA, "F"), author4 = c("F", NA, NA)), row.names = c(NA, 
-3L), class = "data.frame")