R:基于ID的组合

时间:2016-04-09 13:09:03

标签: r combinations

我想为协作网络创建边缘列表。 我有:

dat <- read.table(header=T, text="country ID1
China 1
France 1
Germany 1
'South Africa' 1
Canada 2
Germany 2
'United Kingdom' 2", stringsAsFactors=F)

需要(可能不再是ID):

China France 1
China Germany 1
China South Africa 1
France Germany 1
France South Africa 1
Germany South Africa 1
Canada Germany 2
Canada United Kingdom 2
Germany United Kingdom 2

2 个答案:

答案 0 :(得分:1)

也许这会有所帮助 - 虽然它有点冗长:

numID <- max(df1$ID1)
clist <- list()
for (i in 1:numID) {clist[[i]] <- t(combn(as.character(df1$country[df1$ID1==i]),2))}
as.data.frame(do.call(rbind,clist))
#        V1             V2
# 1   China         France
# 2   China        Germany
# 3   China   South Africa
# 4  France        Germany
# 5  France   South Africa
# 6 Germany   South Africa
# 7  Canada        Germany
# 8  Canada United Kingdom
# 9 Germany United Kingdom

数据:

df1 <- structure(list(country = structure(c(2L, 3L, 4L, 5L, 1L, 4L, 
6L), .Label = c("Canada", "China", "France", "Germany", "South Africa", 
"United Kingdom"), class = "factor"), ID1 = c(1L, 1L, 1L, 1L, 
2L, 2L, 2L)), .Names = c("country", "ID1"), class = "data.frame", 
row.names = c(NA, -7L))

答案 1 :(得分:0)

如果您的最终目标是从边缘列表构建二分网络:

示例数据:

df1 <- structure(list(country = structure(c(2L, 3L, 4L, 5L, 1L, 4L, 
6L), .Label = c("Canada", "China", "France", "Germany", "South Africa", 
"United Kingdom"), class = "factor"), ID1 = c(1L, 1L, 1L, 1L, 
2L, 2L, 2L)), .Names = c("country", "ID1"), class = "data.frame", 
row.names = c(NA, -7L))

转换代码:

library(network)
# construct a bipartite network
net_bp<-as.network(df1,matrix.type='edgelist',bipartite=2,directed=FALSE)
# print the rectangular bipartite network
as.matrix(net_bp)
  Canada China France Germany South Africa United Kingdom
1      0     1      1       1            1              0
2      1     0      0       1            0              1
# convert 1-mode projection on countries
tcrossprod( t(as.matrix(net_bp)) )
               Canada China France Germany South Africa United Kingdom
Canada              1     0      0       1            0              1
China               0     1      1       1            1              0
France              0     1      1       1            1              0
Germany             1     1      1       2            1              1
South Africa        0     1      1       1            1              0
United Kingdom      1     0      0       1            0              1

https://solomonmessing.wordpress.com/2012/09/30/working-with-bipartiteaffiliation-network-data-in-r/

处使用二分网络的一些不错的其他示例