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

Liviu Andronic landronimirc at gmail.com
Thu Sep 1 20:00:07 CEST 2011


On Thu, Sep 1, 2011 at 7:53 PM, Changbin Du <changbind at 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), function(y) mean(y$g))
   let g V1
1    a 1  1
2    a 2  2
3    b 1  1
4    b 2  2
5    c 1  1
6    c 2  2
7    d 1  1
8    d 2  2
9    e 1  1
10   e 2  2

This splits the df in groups of unique 'let' and 'g', and computes the
mean for each such group.

Regards
Liviu



More information about the R-help mailing list