-5

So I know that the following command would store all possible combinations of a desired length y in a list, where y < j:

 lapply(y, function(x) combn(j,x))

But I don't want them all to be stored in a list because later on I will be accessing them only once so it's not efficient to store them in memory. Is there a way where I can just produce each combination in some sort of a loop or something, and then after I'm done performing a calculation, it would just give me the next combination? So basically I want to produce the combinations iteratively instead of storing them first.

So in pseudo code, what i'd like to have is:

#loop that will generate each possible combination one by one
loop{
  operation that uses combination
}
sgibb
  • 25,396
  • 3
  • 68
  • 74
user2560984
  • 361
  • 2
  • 6
  • 12
  • 2
    Provide more code showing the before and after portion of what you want to achieve and you will probably get a good answer (you might get a good one anyway, but it will certainly help). See [**how to make a great reproducible example**](http://stackoverflow.com/q/5963269/1478381) for more info. – Simon O'Hanlon Jul 11 '13 at 13:16

1 Answers1

12

No need for loops (lapply or otherwise):

combn(1:4,2)
#      [,1] [,2] [,3] [,4] [,5] [,6]
# [1,]    1    1    1    2    2    3
# [2,]    2    3    4    3    4    4

Example with calculating the sums of combinations:

combn(1:4,2,FUN=sum)
# [1] 3 4 5 5 6 7

An example with a user defined function:

x <- 11:14
combn(1:4,2,FUN=function(i,a) sum(a[i]),a=x)
#[1] 23 24 25 25 26 27

Here (in the anonymous function) i is the combination used as index and argument a is a vector to which I pass x.

And the same with a user-defined named function:

fun <- function(i,a) sum(a[i])
combn(1:4,2,FUN=fun,a=x)
Roland
  • 127,288
  • 10
  • 191
  • 288
  • ^not quite what I was looking for – user2560984 Jul 11 '13 at 13:22
  • 8
    @user2560984 how is *anyone* supposed to know what you are looking for? You haven't provided a good example. `loop{ operation that uses combination }` tells us nothing. Did you even read the link I posted? In the absence of further information Roland has answered the question you have posted. – Simon O'Hanlon Jul 11 '13 at 13:25
  • 1
    Well then explain better what you are looking for and show input and desired output. – Roland Jul 11 '13 at 13:25
  • ok so in the first piece of code you provided, it generates all combinations at once, but I want to produce them one at a time so it would save more memory... – user2560984 Jul 11 '13 at 13:29
  • 1
    Okay, but what do you do with the results of your function (or "operation") for each combination? Store them? Then you can use the second command (which is the important part of my answer). Otherwise you need to give more details, what you actually want to do. – Roland Jul 11 '13 at 13:31
  • Oohh so in that second piece of code, where you apply the function, is it possible to apply a custom function? Like what if I want the function to have the parameters be the result of the combn? How would I do that? To be more specific, each combination that is generated represents indicies that I would like to use to subset a larger list. So is there a way where I can use combn, and for each combination produced it uses that to subset a larget list? – user2560984 Jul 11 '13 at 13:36
  • Ok now im lost, I apologize for being so demanding, but could you possible provide me with an example in which the function specified uses the combn as indicies? – user2560984 Jul 11 '13 at 13:46
  • so do I need to define the function in the combn() or can it be defined somewhere else and I just call it? Like im not sure why you have the sum() in the combn()? – user2560984 Jul 11 '13 at 13:48
  • Alright, so I tried running that code, but instead of defining the function in the combn(), i defined it else where and I got the error: object 'i' not found – user2560984 Jul 11 '13 at 13:55
  • So this is what I currently have: f <- function(i,x){ return(sum(x[i])) } and when I run this: combn(4,2,FUN=f,x=x), I get the error: "Error in FUN(x[a], ...) : argument "x" is missing, with no default" – user2560984 Jul 11 '13 at 14:00