5

I have data like this:

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 would need it as a list of edges like this:

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

the first three edges coming from ID1, fourth from ID2, ID3 has no edges so nothing from that and fifth from ID4. Any ideas on how to accomplish this? melt/cast?

1

3 Answers 3

8

I'd guess there should be a simple igrpah solution for this, but here's a simple solution using 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
3
  • Nice answer. :) I remember your comment related to if(...) else(...) this week or last week. You were wondering why else() was not in a data.table solution. I cannot recall which question that was. Did you find the reason why one does not need else() part? If you have information, I would like to know it.
    – jazzurro
    Commented Feb 8, 2015 at 13:19
  • @jazzurro I was wondering about if when you want to make an operation such as dplyr::mutate and you have to get values for else too, otherwise you won't have "enough" values. In this situation I'm doing something similar to dplyr::summarise, so I don't need else values (I actually want to get rid of them, thus the if). The solution for the question aksed back then (I guess) is that the OP wanted NAs in the else statement, and when if is running within data.table environment and assfined by := operator, it generates NAs by default (if else isn't provided). Commented Feb 8, 2015 at 13:24
  • 1
    Thank you very much for the clear explanation. The default generation of NA is something good to know. Once again, thank you for taking your time.
    – jazzurro
    Commented Feb 8, 2015 at 13:36
3

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
  • Thanks! I'm using your previous version: lst <- lapply(split(item, DG), function(x) if(length(x) >=2) t(combn(x,2)) else NULL) nodes=as.data.frame(do.call(rbind,lst[!sapply(lst, is.null)]) ), but could You please advise me in how "take along" ID and some other variables (age, sex etc) from the original df and have them as columns in "nodes"?
    – ElinaJ
    Commented Feb 11, 2015 at 7:03
  • @ElinaJ Could you please update your post with the new dataset and expected result
    – akrun
    Commented Feb 11, 2015 at 7:37
  • To be clear and match the answers, I made a new topic: stackoverflow.com/questions/28449118/…
    – ElinaJ
    Commented Feb 11, 2015 at 7:55
2

Here is a more scalable solution that uses the same core logic as the 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)))
1
  • 1
    You could do this within dplyr using do dfPaths %>% group_by(ID) %>% filter(n()>1) %>% do(data.frame(V1=combn(as.character(.$item), 2, FUN=paste, collapse=";")))
    – akrun
    Commented Feb 8, 2015 at 14:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.