[英]Find combinations in choose(k, n) in a R loop
I need to find the SUM of all possible combinations in a set.我需要找到一组中所有可能组合的总和。 For example:
例如:
sum(choose(5, 0),
choose(5, 1),
choose(5, 2),
choose(5, 3),
choose(5, 4),
choose(5, 5))
Instead of writing it out like that, is possible to use a for loop to loop through choose()
with any given n
and k
?可以使用 for 循环来循环
choose()
任何给定的n
和k
而不是这样写出来吗?
You can use您可以使用
> sum(choose(5, 0:5))
[1] 32
or just apply the binomial sums或者只是应用二项式和
> 2^5
[1] 32
In case you want to do this for more than just one n
you can use sapply
如果您想为不止一个
n
执行此操作,您可以使用sapply
setNames( sapply( 1:10, function(x) sum( choose( x, 0:x ) ) ), 1:10 )
1 2 3 4 5 6 7 8 9 10
2 4 8 16 32 64 128 256 512 1024
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.