[R] Creating dataframe of all possible variable combinations

Erik Iverson eriki at ccbr.umn.edu
Thu Mar 25 22:15:41 CET 2010


Hello,

Hosack, Michael wrote:
> 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.

I don't know what you see, since you don't provide reproducible code.  I 
cannot reproduce what you saw.

tmp <- expand.grid(site = c(101,102,103,104),
                    wday = c("MON","TUE","WED","THR","FRI"),
                    tod  = c ("MORN", "AFTN"))

tmp[order(tmp$site),]



More information about the R-help mailing list