Converting a two-mode edge list to a one-mode edge list in R -
Converting a two-mode edge list to a one-mode edge list in R -
i have info set looks this:
person team 10 100 11 100 12 100 10 200 11 200 14 200 15 200
i want infer knew 1 another, based on teams on together. in other words, want create info set looks this:
person1 person2 count 10 11 2 10 12 1 11 12 1 10 14 1 10 15 1 11 14 1 11 15 1
the resulting info set captures dyadic relationships can inferred based on teams outlined in original info set. "count" variable reflects number of instances dyad on team together. also, doesn't matter id listed person1 versus person2, since relationships undirected.
one option:
do.call(rbind,tapply(dat$person,dat$team,function(x)t(combn(x,2)))) # [,1] [,2] # [1,] 10 11 # [2,] 10 12 # [3,] 11 12 # [4,] 11 13 # [5,] 11 14 # [6,] 11 15 # [7,] 13 14 # [8,] 13 15 # [9,] 14 15
edit after op edit : personaly utilize specified bundle igraph package
here can in 2 steps.
res <- setnames(do.call(rbind.data.frame, tapply(dat$person,dat$team, function(x)t(combn(x,2)))), c('person1','person2')) ## compute frequencies of each pair , add together unique version of res cbind(unique(res), count=as.vector(table(paste(res[,'person1'],res[,'person2'])))) # person1 person2 count # 100.1 10 11 2 # 100.2 10 12 1 # 100.3 11 12 1 # 200.2 10 14 1 # 200.3 10 15 1 # 200.4 11 14 1 # 200.5 11 15 1 # 200.6 14 15 1
r
Comments
Post a Comment