Is the a way to generalize expand.grid

I was interested in looking at doing some computations for a bunch of variables whose values were all either 1 or 2.

I was trying to set this up with something like expand grid.

Ideally I could get 10+ columns (column names don't matter).

If I were to do this manually for 3 columns, it'd look something like

df <- expand.grid(a=1:2, b=1:2, c=1:2,)

but this somehow feels inelegant.

I tried using

df <- expand.grid(rep(1:2, 3))

But this produced a single column where the numbers 1 and 2 were repeated 3 times, instead of a grid.

Is there a way to get expand.grid to work with my input, or is there an easy way of doing what I'm asking for using another method?

Something like that?, list(a=1:2, b=1:2, c=1:2))

And if you want to auto-generate the list:, replicate(3, 1:2, simplify = FALSE))
1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.

Perfect! Thanks so much for the prompt reply!