How to use expand.grid on a list of vectors?

Viewed 255

Expand.grid is simple to use, but it requires entering the specific vectors:

a = 1:5
b = 2:5
c = 3:5
df = expand.grid(a,b,c)

But a b and c are arbitrary and are not real names, how can I use it for unknown variables or a list. I tried:

rm(list = ls())
a = 1:5
b = 2:5
c = 3:5
l = ls()
[1] "a" "b" "c"
get(l[1])
[1] 1 2 3 4 5
df = expand.grid(get(l))

L is the list of variables and get gives pulls the variables by name so I expected the two to give the same result but it gives the result of only a.

How can I get the desired result for expand.grid without entering static values?

1 Answers

We can use mget instead of get (as get returns only a single object) to return multiple object values

expand.grid(mget(l))
Related