[R] Creating dataframe of all possible variable combinations

Stephan Kolassa Stephan.Kolassa at gmx.de
Thu Mar 25 21:37:19 CET 2010


Hi Mike,

the following works for me:

SITE <- ordered(c(101,102,103,104))
WDAY <- 
ordered(c("MON","TUE","WED","THR","FRI"),levels=c("MON","TUE","WED","THR","FRI"))
TOD <- ordered(c("MORN","AFTN"),levels=c("MORN","AFTN"))

foo <- expand.grid(SITE=SITE,WDAY=WDAY,TOD=TOD)
foo[order(foo$SITE),]

If this doesn't solve your problem, perhaps you could give us a minimal 
code snippet that demonstrates what exactly you are doing without 
getting what you are looking for?

Bye,
Stephan


Hosack, Michael schrieb:
> Hello,
> 
> I need to create a dataframe containing all possible combinations of
> three variables: SITE (101,102,103,104), WDAY (MON,TUE,WED,THR,FRI),
> and TOD (MORN, AFTN). There should be a total of 40 unique
> combinations in my dataframe. I used expand.grid() successfully(?) to
> create my dataframe, but then when I went to order it by SITE, the
> resultant dataframe only contained four rows, one for each site.
> There must be something about this function that I don't understand.
> Any advice would be appreciated.
> 
> Thank you,
> 
> Mike
> 
> ______________________________________________ R-help at r-project.org
> mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do
> read the posting guide http://www.R-project.org/posting-guide.html 
> and provide commented, minimal, self-contained, reproducible code.
>



More information about the R-help mailing list