I am evaluating if a list of pathways and its genes has pathways included in another pathways. So the way I thought about it is to create a function that does just a single comparison.
all_in <- function(x, y) {
if (length(x) > length(y)) {
0
} else {
ifelse(all(x %in% y), 1, 0)
}
}
And then Vectorize to be able to use outer
all_in_vec <- Vectorize(all_in, vectorize.args = c("x", "y"))
And use it as follows:
nested <- outer(paths2genes, paths2genes, all_in_vec)
example:
paths2genes <- structure(list(`1430728` = c("10", "9"), `156580` = c("10", "3",
"9"), `156582` = c("10", "9"), `194840` = c("2", "3"), `211859` = c("10",
"9")), .Names = c("1430728", "156580", "156582", "194840", "211859"
))
Tests:
library(testthat)
expect_true(all(diag(nested) == 1L))
expect_equal(nested[1, 2], 1L)
However I have recently found that Vectorize should be avoided in favor of a built-in vectorization. Is there any built-in method I am missing? How can I vectorize this code ?