combn

Intersect all possible combinations of list elements

孤街浪徒 提交于 2019-11-29 13:25:38
I have a list of vectors: > l <- list(A=c("one", "two", "three", "four"), B=c("one", "two"), C=c("two", "four", "five", "six"), D=c("six", "seven")) > l $A [1] "one" "two" "three" "four" $B [1] "one" "two" $C [1] "two" "four" "five" "six" $D [1] "six" "seven" I would like to calculate the length of the overlap between all possible pairwise combinations of the list elements , i.e. (the format of the result doesn't matter): AintB 2 AintC 2 AintD 0 BintC 1 BintD 0 CintD 1 I know combn(x, 2) can be used to get a matrix of all possible pairwise combinations in a vector and that length(intersect(a,

Intersect all possible combinations of list elements

帅比萌擦擦* 提交于 2019-11-28 07:22:33
问题 I have a list of vectors: > l <- list(A=c("one", "two", "three", "four"), B=c("one", "two"), C=c("two", "four", "five", "six"), D=c("six", "seven")) > l $A [1] "one" "two" "three" "four" $B [1] "one" "two" $C [1] "two" "four" "five" "six" $D [1] "six" "seven" I would like to calculate the length of the overlap between all possible pairwise combinations of the list elements , i.e. (the format of the result doesn't matter): AintB 2 AintC 2 AintD 0 BintC 1 BintD 0 CintD 1 I know combn(x, 2) can

Faster version of combn

拜拜、爱过 提交于 2019-11-26 11:50:59
Is there a way to speed up the combn command to get all unique combinations of 2 elements taken from a vector? Usually this would be set up like this: # Get latest version of data.table library(devtools) install_github("Rdatatable/data.table", build_vignettes = FALSE) library(data.table) # Toy data d <- data.table(id=as.character(paste0("A", 10001:15000))) # Transform data system.time({ d.1 <- as.data.table(t(combn(d$id, 2))) }) However, combn is 10 times slower (23sec versus 3 sec on my computer) than calculating all possible combinations using data.table. system.time({ d.2 <- d[, list

Faster version of combn

99封情书 提交于 2019-11-26 02:36:53
问题 Is there a way to speed up the combn command to get all unique combinations of 2 elements taken from a vector? Usually this would be set up like this: # Get latest version of data.table library(devtools) install_github(\"Rdatatable/data.table\", build_vignettes = FALSE) library(data.table) # Toy data d <- data.table(id=as.character(paste0(\"A\", 10001:15000))) # Transform data system.time({ d.1 <- as.data.table(t(combn(d$id, 2))) }) However, combn is 10 times slower (23sec versus 3 sec on my