I have to compare all the elements in the list with each other, dog vs cat, dog vs mouse, cat vs mouse.
animals <- list(
dog = data.frame(
col_1 = sample(c(100:200), size = 10, replace = TRUE),
col_2 = sample(c(100:200), size = 10, replace = TRUE),
col_3 = sample(c(100:200), size = 10, replace = TRUE),
col_4 = sample(c(100:200), size = 10, replace = TRUE)
),
cat = data.frame(
col_1 = sample(c(100:200), size = 10, replace = TRUE),
col_2 = sample(c(100:200), size = 10, replace = TRUE),
col_3 = sample(c(100:200), size = 10, replace = TRUE),
col_4 = sample(c(100:200), size = 10, replace = TRUE)
),
mouse = data.frame(
col_1 = sample(c(100:200), size = 10, replace = TRUE),
col_2 = sample(c(100:200), size = 10, replace = TRUE),
col_3 = sample(c(100:200), size = 10, replace = TRUE),
col_4 = sample(c(100:200), size = 10, replace = TRUE)
)
)
I want to compare every combination of two dataframes from the above. The type of comparison isn't important - it's sufficient to say this can be done via a generic_function() like so:
generic_function(animals$dog[, c("col_1", "col_2")],
animals$cat[, c("col_1", "col_2")])
Since there is a lot of data to compare, I would like to use lapply function, but I don't know how to create the intersections between the various levels of the list. Can you help me?
You can generate combinations with
combn()and then iterate over combinations usinglapply():Created on 2024-03-14 with reprex v2.1.0