Combine vectors for factorial designs in R

197 views Asked by At

I would like to create a table that combines multiple vectors in R.

vec1<-c("a", "b")
vec2<-seq(2,4)
vec3<-c("x", "y")

It should look like this:

vec1 vec2 vec3
a    2    x
a    2    y
a    3    x
a    3    y
a    4    x
a    4    y
b    2    x
b    2    y
b    3    x
b    3    y
b    4    x
b    4    y

I tried to use outer. It does what I want when I have two vectors, but not when I have more than two vectors.

1

There are 1 answers

0
JasonAizkalns On BEST ANSWER

Being more explicit:

expand.grid(vec1, vec2, vec3)