[R] expand.grid() function

Ken Knoblauch ken.knoblauch at inserm.fr
Mon Jun 23 15:54:34 CEST 2008


Megh Dal <megh700004 <at> yahoo.com> writes:
>   I have one question on expand.grid() function.
>   When I write following syntax :expand.grid(c("u", "l"), 
>c("u", "l"), c("u", "l")) I get following as
> desired :
>     Var1 Var2 Var3
> 1    u    u    u
> 2    l    u    u
> 3    u    l    u
> 4    l    l    u
> 5    u    u    l
> 6    l    u    l
> 7    u    l    l
> 8    l    l    l
>   However I wanted to write that in more concise manner. 
>Therefore I tried : expand.grid(rep(c("u", "l"),
> 3)). But I did not get answer that I previously got. 
>Can people here clarify me why it is not like that? >Then
> what would be the mose concise way to do that?
Just to put this under the correct subject heading, 
I repeat it here.

How about

do.call("expand.grid", rep(list(c("u", "l")), 3))
  Var1 Var2 Var3
1    u    u    u
2    l    u    u
3    u    l    u
4    l    l    u
5    u    u    l
6    l    u    l
7    u    l    l
8    l    l    l

Sorry about the incorrect posting...



More information about the R-help mailing list