Reputation: 38
If there are two lists of the form
list_a <- list(c(1,2,3,4,5), c(6,7,8,9,10), c(11,12,13,14))
list_b <- list(c(1,9,3,7,5), c(2,7,13,9,10), c(1,12,5,14))
I want to compare the elements in list_a
vs the elements in list list_b
and extract the sum of matched elements. I mean, I want to compare as follows
sum(c(1,2,3,4,5) %in% c(1,9,3,7,5))
sum(c(1,2,3,4,5) %in% c(2,7,13,9,10))
sum(c(1,2,3,4,5) %in% c(1,12,5,14))
sum(c(6,7,8,9,10) %in% c(1,9,3,7,5))
sum(c(6,7,8,9,10) %in% c(2,7,13,9,10))
sum(c(6,7,8,9,10) %in% c(1,12,5,14))
sum(c(11,12,13,14) %in% c(1,9,3,7,5))
sum(c(11,12,13,14) %in% c(2,7,13,9,10))
sum(c(11,12,13,14) %in% c(1,12,5,14))
I tried the following code using the sapply()
function and the output is as expected (Colums maps to elements in list_a
and rows maps to elements in list_b
). However, when the length of the lists is large this code is too slow (Imagine list_a
with 10000 elements and list_b
with 10000 elements).
test <- sapply(list_a, function(x){
out_sum <- sapply(list_b, function(y){
matches <- sum(x %in% y)
return(matches)
})
return(out_sum)
})
Output
Does anyone have an idea?
Upvotes: 0
Views: 296
Reputation: 388982
Another option is to use outer
-
check <- function(x, y) sum(list_a[[x]] %in% list_b[[y]])
test <- outer(seq_along(list_a), seq_along(list_b), Vectorize(check))
Upvotes: 2
Reputation: 168
You can try using the map
function. It reduces the runtime by more than half.
library(purrr)
out_sum <- list_b %>% map(function (y) {
list_a %>% map(function(x) sum(x %in% y))
})
out_matrix <- matrix(unlist(out_sum), ncol = length(list_a), byrow = TRUE)
Upvotes: 1