All possible combinations of elements from different bins (one element from every bin) [duplicate]

安稳与你 提交于 2019-12-25 20:00:32

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!