【问题标题】:All possible combinations of elements from different bins (one element from every bin) [duplicate]来自不同箱的元素的所有可能组合(每个箱中的一个元素)[重复]
【发布时间】:2016-04-25 10:38:38
【问题描述】:

我有一个列表,其中每个元素都是一组数字。所有集合的长度都不同:

 a <- list(1,c(2,3),c(4,5,6))
#> a
#[[1]]
#[1] 1

#[[2]]
#[1] 2 3

#[[3]]
#[1] 4 5 6 

我想从每个集合中获取一个元素的所有可能组合。在这个例子中应该是:

1 2 4, 1 2 5, 1 2 6, 1 3 4, 1 3 5, 1 3 6

我觉得这里 *apply-functions 的一些组合会很有用,但不知道该怎么做。

【问题讨论】:

    标签: r combinatorics lapply sapply


    【解决方案1】:

    我们可以使用expand.grid

    expand.grid(a)
    

    【讨论】:

    • 不错!不能要求更短的解决方案:)
    猜你喜欢
    • 1970-01-01
    • 2018-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多