问题
I have a list, where each element is a set of numbers. Lengths of all sets are different:
a <- list(1,c(2,3),c(4,5,6))
#> a
#[[1]]
#[1] 1
#[[2]]
#[1] 2 3
#[[3]]
#[1] 4 5 6
I'd like to get all possible combinations of one element from each set. In this example it should be:
1 2 4, 1 2 5, 1 2 6, 1 3 4, 1 3 5, 1 3 6
I feel that some combination of *apply-functions here would be useful, but can't figure out how to do that.
回答1:
We can use expand.grid
expand.grid(a)
来源:https://stackoverflow.com/questions/34885551/all-possible-combinations-of-elements-from-different-bins-one-element-from-ever