[R] Creating dataframe of all possible variable combinations
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@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.
[R] Creating dataframe of all possible variable combinations
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@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.
Re: [R] Creating dataframe of all possible variable combinations
Hi Mike, Try this: SITE - c(101,102,103,104) WDAY - c('MON','TUE','WED','THR','FRI') TOD - c('MORN', 'AFTN') out - expand.grid(SITE, WDAY, TOD) out HTH, Jorge On Thu, Mar 25, 2010 at 4:21 PM, 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. Thank you, Mike __ R-help@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. [[alternative HTML version deleted]] __ R-help@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.
Re: [R] Creating dataframe of all possible variable combinations
On 26/03/2010, at 9:22 AM, 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. Works for me. I used: xxx - expand.grid(SITE=SITE,WDAY=WDAY,TOD=TOD) yyy - xxx[order(xxx$SITE),] I expect you used: yyy - xxx[order(SITE),] In the foregoing, SITE is the length-4 vector in your global environment, not the length-40 column of xxx. So ``naturally'' you get a result with 4 rows. cheers, Rolf Turner ## Attention:\ This e-mail message is privileged and confid...{{dropped:9}} __ R-help@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.
Re: [R] Creating dataframe of all possible variable combinations
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@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. __ R-help@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.
Re: [R] Creating dataframe of all possible variable combinations
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),] __ R-help@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.