如何将14个数字组合的所有组合列表分为两个单独的组?

发布于 2025-02-08 04:52:31 字数 423 浏览 3 评论 0原文

因此,我正在尝试创建所有14个数字组合的列表,这些列表已分为两个单独的组。例如,此列表的开头看起来像:

X组Y
12、3、4、5、6、7、8、9、10、11、11、12、13、14
1、23、4、5 ,6、7、8、9、10、11、12、13、14 1、2、3
4、5、6、7、8、9、10、11、11、12、13、14

类的。当分为两组时,此列表将包括16384个可能的组合。 (2^14)关于如何在R中执行此操作的任何想法?

So I am trying to create a list of all combinations of 14 numbers that have been split into two separate groups. For example, the beginning of this list may look like:

Group XGroup Y
12, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14
1, 23, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14
1, 2, 34, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14

And so forth. This list would consist of 16384 possible combinations of these 14 numbers when split into two groups. (2^14) Any ideas on how to do this in R?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

挽清梦 2025-02-15 04:52:31

您可以尝试(对于2^3案例)

x <- expand.grid(replicate(3, c(T,F), simplify = F))
apply(x, 1, function(x){list(group1 = c(1:3)[x], group2 = c(1:3)[!x])})

[[1]]
[[1]]$group1
[1] 1 2 3

[[1]]$group2
numeric(0)


[[2]]
[[2]]$group1
[1] 2 3

[[2]]$group2
[1] 1


[[3]]
[[3]]$group1
[1] 1 3

[[3]]$group2
[1] 2


[[4]]
[[4]]$group1
[1] 3

[[4]]$group2
[1] 1 2


[[5]]
[[5]]$group1
[1] 1 2

[[5]]$group2
[1] 3


[[6]]
[[6]]$group1
[1] 2

[[6]]$group2
[1] 1 3


[[7]]
[[7]]$group1
[1] 1

[[7]]$group2
[1] 2 3


[[8]]
[[8]]$group1
numeric(0)

[[8]]$group2
[1] 1 2 3

You may try (for 2^3 cases)

x <- expand.grid(replicate(3, c(T,F), simplify = F))
apply(x, 1, function(x){list(group1 = c(1:3)[x], group2 = c(1:3)[!x])})

[[1]]
[[1]]$group1
[1] 1 2 3

[[1]]$group2
numeric(0)


[[2]]
[[2]]$group1
[1] 2 3

[[2]]$group2
[1] 1


[[3]]
[[3]]$group1
[1] 1 3

[[3]]$group2
[1] 2


[[4]]
[[4]]$group1
[1] 3

[[4]]$group2
[1] 1 2


[[5]]
[[5]]$group1
[1] 1 2

[[5]]$group2
[1] 3


[[6]]
[[6]]$group1
[1] 2

[[6]]$group2
[1] 1 3


[[7]]
[[7]]$group1
[1] 1

[[7]]$group2
[1] 2 3


[[8]]
[[8]]$group1
numeric(0)

[[8]]$group2
[1] 1 2 3
羁〃客ぐ 2025-02-15 04:52:31

我相信此代码可以完成与您要求的有关的事情。结果通过相应的行存储在矩阵“ group1”和“ group2”的非零条目中,因此您可能需要更改代码,以便以对您的目的有用的形式呈现结果。

输出从R控制台返回时显示出来;末尾的截断数据表说明了输出的形式。

# Written in R version 4.1.1
require(data.table)

group1 = matrix(data = 0, nrow = 16384, ncol = 14, byrow = T)
group2 = matrix(data = 0, nrow = 16384, ncol = 14, byrow = T)
a = 1:14
count = 1
for(i in 0:14){
  nf = data.frame(combn(a,i))
    for(j in 1:dim(nf)[2]){
        g1 = c(nf[,j])
        if(length(g1) > 0){
          group1[count,1:length(g1)] = g1
          }
          g2 = a[-g1]
          if(length(g2) > 0){
            group2[count,1:length(g2)] = g2
            }
          if(i == 0 & j == 1){
            group2[count,1:14] = 1:14
            }
            count = count + 1
             }}
data.table(group1)
# V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 V11 V12 V13 V14
# 1:  0  0  0  0  0  0  0  0  0   0   0   0   0   0
# 2:  1  0  0  0  0  0  0  0  0   0   0   0   0   0
# 3:  2  0  0  0  0  0  0  0  0   0   0   0   0   0
# 4:  3  0  0  0  0  0  0  0  0   0   0   0   0   0
# 5:  4  0  0  0  0  0  0  0  0   0   0   0   0   0
# ---                                               
# 16380:  1  2  3  5  6  7  8  9 10  11  12  13  14   0
# 16381:  1  2  4  5  6  7  8  9 10  11  12  13  14   0
# 16382:  1  3  4  5  6  7  8  9 10  11  12  13  14   0
# 16383:  2  3  4  5  6  7  8  9 10  11  12  13  14   0
# 16384:  1  2  3  4  5  6  7  8  9  10  11  12  13  14
data.table(group2)
# V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 V11 V12 V13 V14
# 1:  1  2  3  4  5  6  7  8  9  10  11  12  13  14
# 2:  2  3  4  5  6  7  8  9 10  11  12  13  14   0
# 3:  1  3  4  5  6  7  8  9 10  11  12  13  14   0
# 4:  1  2  4  5  6  7  8  9 10  11  12  13  14   0
# 5:  1  2  3  5  6  7  8  9 10  11  12  13  14   0
# ---                                               
# 16380:  4  0  0  0  0  0  0  0  0   0   0   0   0   0
# 16381:  3  0  0  0  0  0  0  0  0   0   0   0   0   0
# 16382:  2  0  0  0  0  0  0  0  0   0   0   0   0   0
# 16383:  1  0  0  0  0  0  0  0  0   0   0   0   0   0
# 16384:  0  0  0  0  0  0  0  0  0   0   0   0   0   0
print(paste("count = ",count - 1))
# [1] "count =  16384"

I believe that this code accomplishes something close to what you are requesting. The results are stored in the non-zero entries of the matrices "group1" and "group2" by corresponding row, so you may need to alter the code in order to render the results in a form that is useful for your purposes.

The output is shown as it was returned from the R console; the truncated data tables at the end illustrate the form of the output.

# Written in R version 4.1.1
require(data.table)

group1 = matrix(data = 0, nrow = 16384, ncol = 14, byrow = T)
group2 = matrix(data = 0, nrow = 16384, ncol = 14, byrow = T)
a = 1:14
count = 1
for(i in 0:14){
  nf = data.frame(combn(a,i))
    for(j in 1:dim(nf)[2]){
        g1 = c(nf[,j])
        if(length(g1) > 0){
          group1[count,1:length(g1)] = g1
          }
          g2 = a[-g1]
          if(length(g2) > 0){
            group2[count,1:length(g2)] = g2
            }
          if(i == 0 & j == 1){
            group2[count,1:14] = 1:14
            }
            count = count + 1
             }}
data.table(group1)
# V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 V11 V12 V13 V14
# 1:  0  0  0  0  0  0  0  0  0   0   0   0   0   0
# 2:  1  0  0  0  0  0  0  0  0   0   0   0   0   0
# 3:  2  0  0  0  0  0  0  0  0   0   0   0   0   0
# 4:  3  0  0  0  0  0  0  0  0   0   0   0   0   0
# 5:  4  0  0  0  0  0  0  0  0   0   0   0   0   0
# ---                                               
# 16380:  1  2  3  5  6  7  8  9 10  11  12  13  14   0
# 16381:  1  2  4  5  6  7  8  9 10  11  12  13  14   0
# 16382:  1  3  4  5  6  7  8  9 10  11  12  13  14   0
# 16383:  2  3  4  5  6  7  8  9 10  11  12  13  14   0
# 16384:  1  2  3  4  5  6  7  8  9  10  11  12  13  14
data.table(group2)
# V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 V11 V12 V13 V14
# 1:  1  2  3  4  5  6  7  8  9  10  11  12  13  14
# 2:  2  3  4  5  6  7  8  9 10  11  12  13  14   0
# 3:  1  3  4  5  6  7  8  9 10  11  12  13  14   0
# 4:  1  2  4  5  6  7  8  9 10  11  12  13  14   0
# 5:  1  2  3  5  6  7  8  9 10  11  12  13  14   0
# ---                                               
# 16380:  4  0  0  0  0  0  0  0  0   0   0   0   0   0
# 16381:  3  0  0  0  0  0  0  0  0   0   0   0   0   0
# 16382:  2  0  0  0  0  0  0  0  0   0   0   0   0   0
# 16383:  1  0  0  0  0  0  0  0  0   0   0   0   0   0
# 16384:  0  0  0  0  0  0  0  0  0   0   0   0   0   0
print(paste("count = ",count - 1))
# [1] "count =  16384"
-小熊_ 2025-02-15 04:52:31

我们可以尝试combn,如下所示

v <- 1:14
res <- do.call(
  c,
  lapply(
    c(0, seq_along(v)),
    function(k) {
      combn(v, k, function(x) list(grp1 = x, grp2 = v[!v %in% x]),
        simplify = FALSE
      )
    }
  )
)

,我们将看到

> length(res)
[1] 16384


> head(res)
[[1]]
[[1]]$grp1
integer(0)

[[1]]$grp2
 [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14


[[2]]
[[2]]$grp1
[1] 1

[[2]]$grp2
 [1]  2  3  4  5  6  7  8  9 10 11 12 13 14


[[3]]
[[3]]$grp1
[1] 2

[[3]]$grp2
 [1]  1  3  4  5  6  7  8  9 10 11 12 13 14


[[4]]
[[4]]$grp1
[1] 3

[[4]]$grp2
 [1]  1  2  4  5  6  7  8  9 10 11 12 13 14


[[5]]
[[5]]$grp1
[1] 4

[[5]]$grp2
 [1]  1  2  3  5  6  7  8  9 10 11 12 13 14


[[6]]
[[6]]$grp1
[1] 5

[[6]]$grp2
 [1]  1  2  3  4  6  7  8  9 10 11 12 13 14

We can try combn like below

v <- 1:14
res <- do.call(
  c,
  lapply(
    c(0, seq_along(v)),
    function(k) {
      combn(v, k, function(x) list(grp1 = x, grp2 = v[!v %in% x]),
        simplify = FALSE
      )
    }
  )
)

and we will see

> length(res)
[1] 16384


> head(res)
[[1]]
[[1]]$grp1
integer(0)

[[1]]$grp2
 [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14


[[2]]
[[2]]$grp1
[1] 1

[[2]]$grp2
 [1]  2  3  4  5  6  7  8  9 10 11 12 13 14


[[3]]
[[3]]$grp1
[1] 2

[[3]]$grp2
 [1]  1  3  4  5  6  7  8  9 10 11 12 13 14


[[4]]
[[4]]$grp1
[1] 3

[[4]]$grp2
 [1]  1  2  4  5  6  7  8  9 10 11 12 13 14


[[5]]
[[5]]$grp1
[1] 4

[[5]]$grp2
 [1]  1  2  3  5  6  7  8  9 10 11 12 13 14


[[6]]
[[6]]$grp1
[1] 5

[[6]]$grp2
 [1]  1  2  3  4  6  7  8  9 10 11 12 13 14
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文