r - Count number of connenction in data.frame dplyr -
i have data.frame:
df <- data.frame(id = rep(1:4, each = 3),x = c("a","b","c","d","e","a","a","c","d","a","c","e"))
i want count connections inside each id: output want get:
connections |num. of connections - b | 1 b - c | 1 c - d | 1 - c | 3 - e | 2 - d | 2 d - e | 1 c - e | 1
how in dplyr?
using dplyr
, combn
library(dplyr) df %>% group_by(id) %>% mutate(connections=c(combn(as.character(x),2, fun=function(x) paste(sort(x), collapse=" - ")))) %>% group_by(connections) %>% summarise(numconn=n()) # connections numconn #1 - b 1 #2 - c 3 #3 - d 2 #4 - e 2 #5 b - c 1 #6 c - d 1 #7 c - e 1 #8 d - e 1
or same approach data.table
library(data.table) setdt(df)[,combn(as.character(x),2, fun= function(x) paste(sort(x), collapse=" - ")) , by=id][ ,list(numconn=.n), by=list(connections=v1)] # connections numconn #1: - b 1 #2: - c 3 #3: b - c 1 #4: d - e 1 #5: - d 2 #6: - e 2 #7: c - d 1 #8: c - e 1
Comments
Post a Comment