查找所有由6个数字加起来组成的所有组合的列表 [英] Finding A List of All Combinations of 6 Numbers That Add up to 10

查看:402
本文介绍了查找所有由6个数字加起来组成的所有组合的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我之前曾看到过类似版本的问题(

So I've seen similar versions of this question asked before (Getting all combinations which sum up to 100 using R) but I'm struggling to find a way to figure out what I need to run specifically. I'm trying to create a list in R of all the different combinations of 6 numbers that add up to 10. However, I want to include 0s and repeats of the same # in the row. So it would look something like this:

10 0 0 0 0 0 9 1 0 0 0 0 8 2 0 0 0 0 我尝试运行以下命令:

10 0 0 0 0 0 9 1 0 0 0 0 8 2 0 0 0 0 I've tried running the following:

C = t(restrictedparts(10,6, include.zero=TRUE))
ComboSet<-data.frame(do.call(rbind, lapply(1:nrow(C),function(i) getall(iterpc(table(C[i,]), order=T)))))

但是,当我这样做时,它似乎并未包含其中包含0的变体.我尝试将include.zero = TRUE函数输入到我正在运行的内容的不同部分中,但到目前为止我还没有运气.有什么建议?

However, when I do this it does not seem to include the variations that have 0s in them. I've tried entering the include.zero=TRUE function into different parts of what I'm running but I've had no luck so far. Any suggestions?

推荐答案

这是一个很好的问题,答案不是很明显.

This is a good question and the answer isn't very obvious.

有很多事情要在这里解决.对于初学者,请确保您包括 您在示例中使用的库.我从经验中知道您正在使用partitionsiterpc.从partitions文档中,我们看到有一个函数可以完全返回您要查找的内容,而无需任何其他步骤.这是compositions函数,它生成整数成分.

There are many things to address here. For starters, make sure that you include the libraries that you use in your example. I know from experience that you are using partitions and iterpc. From the partitions documentation, we see that there is a function that returns exactly what you are looking for without any additional steps. It is the compositions function which generates Integer Compositions.

myComps <- t(as.matrix(compositions(10, 6)))
head(myComps)
     [,1] [,2] [,3] [,4] [,5] [,6]
[1,]   10    0    0    0    0    0
[2,]    9    1    0    0    0    0
[3,]    8    2    0    0    0    0
[4,]    7    3    0    0    0    0
[5,]    6    4    0    0    0    0
[6,]    5    5    0    0    0    0

dim(myComps)
[1] 3003    6

all(rowSums(myComps) == 10)
[1] TRUE

至于修复您的实际代码,我不确定为什么您的代码无法按原样工作.我过去使用过iterpc,并且还记得明确地使用过相同的方法.任何人,解决方法是显式声明labels参数,因为正在使用每个元素的频率而不是值本身.

As for fixing your actual code, I'm not exactly sure why your code is not working as is. I've used iterpc in the past and remember explicitly using that same approach. Anywho, the workaround is to explicitly declare the labels parameter as the frequency of each element is being used instead of the value itself.

## The 1's should be 0's and the 2's should be 10's
ComboSet[1:6, ]
X1 X2 X3 X4 X5 X6
1  1  1  1  1  1  2
2  1  1  1  1  2  1
3  1  1  1  2  1  1
4  1  1  2  1  1  1
5  1  2  1  1  1  1
6  2  1  1  1  1  1

## OP's original code
ComboSet<-data.frame(do.call(rbind, lapply(1:nrow(C),function(i) getall(iterpc(table(C[i,]), order=T)))))

all(rowSums(ComboSet) == 10)
[1] FALSE

table(rowSums(ComboSet))

7   8   9  10  11  12  13  14  15  16 
12  30 150 255 186 690 420 420 180 660

## Here is the fix with labels explicitly declared
ComboSetFix <- data.frame(do.call(rbind, lapply(1:nrow(C), function(i) {
    getall(iterpc(table(C[i,]), labels = as.integer(names(table(C[i,]))), order=T))
})))

all(rowSums(ComboSetFix) == 10)
[1] TRUE

dim(ComboSetFix)
[1] 3003    6

您应该知道iterpc没有得到积极维护,鼓励用户切换到arrangements.它具有不同的界面,因此您不能简单地将"iterpc"一词替换为"arrangements".

You should know that iterpc is not being actively maintained and users are encouraged to switch to arrangements. It has a different interface, so you can't simply replace the word "iterpc" with "arrangements".

这篇关于查找所有由6个数字加起来组成的所有组合的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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