来自不同容器的元素的所有可能组合(每个容器一个元素) [英] All possible combinations of elements from different bins (one element from every bin)

查看:56
本文介绍了来自不同容器的元素的所有可能组合(每个容器一个元素)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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想从每个集合中获得一个元素的所有可能组合。在此示例中,应为:

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

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

我觉得这里* apply-functions的某些组合会很有用,但无法弄清楚该怎么做。

I feel that some combination of *apply-functions here would be useful, but can't figure out how to do that.

推荐答案

我们可以使用 expand.grid

expand.grid(a)

这篇关于来自不同容器的元素的所有可能组合(每个容器一个元素)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆