R:从一个向量,列出所有元素的子集,所以他们的总和只是传递一个值 [英] R: from a vector, list all subsets of elements so their sum just passes a value

查看:309
本文介绍了R:从一个向量,列出所有元素的子集,所以他们的总和只是传递一个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果答案(1)不重要,请提前抱歉;或(2)在那里,但我没有能够解决这个问题,或在网上找到和回答。任何指针都将非常感谢!



我需要一段代码,可以通过一个向量,并返回所有可能的元素的子集,其累积和超过一个阈值值。



请注意,我不希望只有给出完全阈值的子集。累积和可以在阈值以上,只要算法停止添加额外的元素,如果值已经实现。

 #输入数据类型的一个小例子。 
#但是,请注意,效率是一个问题
#(我需要在大数据集中复制示例)
v< - seq(1,3)#我的向量
阈值< - 3#我的阈值

#我想获得组合的列表
#1 2
#1 3
#2 3
#3

这段代码是可行的,但却是地球上最诡异的解决方案...

  for(i in 1:length(v)){
thisvalue< - v [i]
if(thisvalue> = threshold){
cat(v [i],\\\
,sep =\t)
} else {
for (b)(b)(b)(b)(b)(c)(c)(c) (k in(i + 2):length(v)),其中v(i + 1,v [i],v [j]),\\\
,sep =\t ){
thisvalue <-v [i] + v [j] + v [k]
if(thisvalue> = threshold){
cat v [j],v [k]),\\\
,sep =\t)b


}
}


解决方案

有限的编码技能可能效率低下,但是比写无尽循环更可行和更简洁。



该函数的灵感来自于
查找汇总的集合的所有子集到n

  recursive.subset< -function(x,index,current,threshold,result){
for(i in index:length(x)){
if(current + x [i]> = threshold){
cat(result,x [i],\\\
,$ sep $ b} else {
recursive.subset(x,i + 1,current + x [i],threshold,c(result,x [i]))
}
}
}

要调用函数

  inivector<  -  vector(mode =numeric,length = 0)#initializing空向量
递归。所以我得到

$ b(v,1,0,threshold,inivector)



$ b

1 2

1 3

2 3

3


Sorry in advance if the answer (1) is trivial; or (2) out there but I haven't been able to solve this issue or find and answer online. Any pointers will be much appreciated!

I am in need of a piece of code that can run through a vector and return all possible subsets of elements whose cumulative sum passes a threshold value.

Note that I do not want only the subsets that give me exactly the threshold. The cumulative sum can be above the threshold, as long as the algorithm stops adding an extra element if the value has been achieved already.

# A tiny example of the kind of input data. 
# However, note that efficiency is an issue 
# (I need to replicate the example in a large dataset)
v <- seq(1, 3) # My vector
threshold <- 3 # My threshold value

# I would like to get a list with the combinations
# 1 2 
# 1 3
# 2 3
# 3 

This piece of code works but is the clunkiest solution on earth...

for (i in 1: length(v)){
  thisvalue <- v[i]
  if (thisvalue >=threshold) { 
    cat (v[i], "\n",sep="\t") 
  } else {
    for (j in (i+1): length(v)){
      thisvalue <- v[i]+v[j]
      if (thisvalue >=threshold) { 
        cat (c(v[i], v[j]), "\n",sep="\t")
      } else {
        for (k in (i+2): length(v)){
          thisvalue <- v[i]+v[j]+v[k]
          if (thisvalue >=threshold) { 
            cat(c(v[i],v[j],v[k]),"\n",sep="\t")
        }
        }
      }
    }
  }
}

解决方案

I found a solution within my limited coding skills that might be inefficient but it is feasible and neater than writing endless loops.

The function was inspired by the java code found at Find all subsets of a set that sum up to n

recursive.subset <-function(x, index, current, threshold, result){
  for (i in index:length(x)){
    if (current + x[i] >= threshold){
      cat (result, x[i], "\n",sep="\t") 
    } else {
      recursive.subset(x, i + 1, current+x[i], threshold, c(result,x[i]))
    }
  }
}

To call the function, just

inivector <- vector(mode="numeric", length=0) #initializing empty vector
recursive.subset (v, 1, 0, threshold, inivector)

So I get

1 2
1 3
2 3
3

这篇关于R:从一个向量,列出所有元素的子集,所以他们的总和只是传递一个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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