-4

I have a list, where each element is a set of numbers. Lengths of all sets are different:

 a <- list(1,c(2,3),c(4,5,6))
#> a
#[[1]]
#[1] 1

#[[2]]
#[1] 2 3

#[[3]]
#[1] 4 5 6 

I'd like to get all possible combinations of one element from each set. In this example it should be:

1 2 4, 1 2 5, 1 2 6, 1 3 4, 1 3 5, 1 3 6

I feel that some combination of *apply-functions here would be useful, but can't figure out how to do that.

Dmitry Shopin
  • 1,753
  • 10
  • 11

1 Answers1

10

We can use expand.grid

expand.grid(a)
akrun
  • 874,273
  • 37
  • 540
  • 662