[R] how to split a data frame by two variables

2011-09-01 Thread Changbin Du
HI, Dear R community, I want to split a data frame by using two variables: let and g x = data.frame(num = c(10,11,12,43,23,14,52,52,12,23,21,23,32,31,24,45,56,56,76,45), let = letters[1:5], g = 1:2) x num let g 1 10 a 1 2 11 b 2 3 12 c 1 4 43 d 2 5 23 e 1 6 14 a 2 7

Re: [R] how to split a data frame by two variables

2011-09-01 Thread jim holtman
try this: split(x, list(x$let, x$g)) $a.1 num let g 1 10 a 1 11 21 a 1 $b.1 num let g 7 52 b 1 17 56 b 1 $c.1 num let g 3 12 c 1 13 32 c 1 $d.1 num let g 9 12 d 1 19 76 d 1 $e.1 num let g 5 23 e 1 15 24 e 1 On Thu, Sep 1, 2011 at 1:53 PM,

Re: [R] how to split a data frame by two variables

2011-09-01 Thread Liviu Andronic
On Thu, Sep 1, 2011 at 7:53 PM, Changbin Du changb...@gmail.com wrote: HI, Dear R community, I want to split a data frame by using two variables: let and g It's not clear what you want to do, but investigate the following: require(plyr) Loading required package: plyr ddply(x, .(let, g),

Re: [R] how to split a data frame by two variables

2011-09-01 Thread David Winsemius
On Sep 1, 2011, at 1:53 PM, Changbin Du wrote: HI, Dear R community, I want to split a data frame by using two variables: let and g x = data.frame(num = c(10,11,12,43,23,14,52,52,12,23,21,23,32,31,24,45,56,56,76,45), let = letters[1:5], g = 1:2) x num let g 1 10 a 1 2 11 b 2 3

Re: [R] how to split a data frame by two variables

2011-09-01 Thread Changbin Du
Thanks for the great helps from David, Jim and Liviu. It solved my problem. Appreciated! On Thu, Sep 1, 2011 at 11:01 AM, David Winsemius dwinsem...@comcast.netwrote: On Sep 1, 2011, at 1:53 PM, Changbin Du wrote: HI, Dear R community, I want to split a data frame by using two variables:

Re: [R] how to split a data frame by two variables

2011-09-01 Thread MacQueen, Don
Even though it's not needed, here's a small followup. I usually use this split(x, paste(x$let,x$g)) But since split(x, list(x$let,x$g)) works, so does split(x, x[,c('let','g')]) all.equal( split(x, x[,c('let','g')]) , split(x,list(x$let,x$g))) [1] TRUE As to which is the best, hard

Re: [R] how to split a data frame by two variables

2011-09-01 Thread Changbin Du
Thanks, Don! Appreciated your detailed explanation. On Thu, Sep 1, 2011 at 11:28 AM, MacQueen, Don macque...@llnl.gov wrote: Even though it's not needed, here's a small followup. I usually use this split(x, paste(x$let,x$g)) But since split(x, list(x$let,x$g)) works, so does