Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
755 views
in Technique[技术] by (71.8m points)

combinations - I want to create an algorithm in R that takes a vector and outputs all the vectors that are subsets of the original

I am trying to create an algorith in R that not only lists all possible subsets of a vector but also allows me to check for certain properties of these subsets, so combn is not sufficient. I tried this:

f2 = function(z)
{
  z1 = 1:z
  for (i in 1:z)
  {
    z1[i] = factorial(z)/((factorial(i-1)*(factorial(z-(i-1)))))
  }
  return(z1)
}

f3 = function(k)
{
  k1 = length(k)
  z1 = f2(k1)
  
  for (i in 2:k1)
  {
    s = combn(1:k1, i, simplify = TRUE)
    z2 = z1[k1-i+1]
    
    for (j in 1:z2)
    {
  
      for (n in 1:i)
      {
        s1[n] = s[n, j]
        print(s1)
      }
    }
  }
  
}

But this just seperates each number. Is there a different approach I can take? Thank you.

question from:https://stackoverflow.com/questions/65948423/i-want-to-create-an-algorithm-in-r-that-takes-a-vector-and-outputs-all-the-vecto

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...