Creating a list of edges in R

I have the following data:

ID=c(rep("ID1",3), rep("ID2",2), "ID3", rep("ID4",2)) item=c("a","b","c","a","c","a","b","a") data.frame(ID,item) ID1 a ID1 b ID1 c ID2 a ID2 c ID3 a ID4 b ID4 a 

and I need this as a list of edges like this:

 a;b b;c a;c a;c b;a 

the first three edges coming from ID1, the fourth from ID2, ID3 have no boundaries, so none of this and the fifth from ID4. Any ideas on how to do this? melt / cast?

+5
source share
3 answers

Try

  res <- do.call(rbind,with(df, tapply(item, ID, FUN=function(x) if(length(x)>=2) t(combn(x,2))))) paste(res[,1], res[,2], sep=";") #[1] "a;b" "a;c" "b;c" "a;c" "b;a" 
+3
source

I would suggest that there should be a simple igrpah solution for this, but here is a simple solution using the data.table package

 library(data.table) setDT(df)[, if(.N > 1) combn(as.character(item), 2, paste, collapse = ";"), ID] # ID V1 # 1: ID1 a;b # 2: ID1 a;c # 3: ID1 b;c # 4: ID2 a;c # 5: ID4 b;a 
+6
source

Here is a more scalable solution using the same basic logic as other solutions:

 library(plyr) library(dplyr) ID=c(rep("ID1",3), rep("ID2",2), "ID3", rep("ID4",2)) item=c("a","b","c","a","c","a","b","a") dfPaths = data.frame(ID, item) dfPaths2 = dfPaths %>% group_by(ID) %>% mutate(numitems = n(), item = as.character(item)) %>% filter(numitems > 1) ddply(dfPaths2, .(ID), function(x) t(combn(x$item, 2))) 
+2
source

Source: https://habr.com/ru/post/1212896/


All Articles