renkun-ken / rlist

A Toolbox for Non-Tabular Data Manipulation
Other
202 stars 28 forks source link

Implement a list version of expand.grid #107

Closed renkun-ken closed 8 years ago

renkun-ken commented 8 years ago

expand.grid exhausts all possible combinations of elements from given vectors and form a data.frame.

> expand.grid(x = c(1,2,3), y = c(2,3,4))
  x y
1 1 2
2 2 2
3 3 2
4 1 3
5 2 3
6 3 3
7 1 4
8 2 4
9 3 4

A list equivalent might be useful in some cases. It should work like

list.expand(x = list(1,2,c(2,3)), y = list("x", c("p", "q")))

which produces a list with the following structure

List of 6
 $ :List of 2
  ..$ x: num 1
  ..$ y: chr "x"
 $ :List of 2
  ..$ x: num 1
  ..$ y: chr [1:2] "p" "q"
 $ :List of 2
  ..$ x: num 2
  ..$ y: chr "x"
 $ :List of 2
  ..$ x: num 2
  ..$ y: chr [1:2] "p" "q"
 $ :List of 2
  ..$ x: num [1:2] 2 3
  ..$ y: chr "x"
 $ :List of 2
  ..$ x: num [1:2] 2 3
  ..$ y: chr [1:2] "p" "q"