[R] collapsing a data frame

Chris Stubben stubben at lanl.gov
Fri Oct 12 22:34:07 CEST 2007


> On 10/12/07, Ben Bolker <bolker <at> ufl.edu> wrote:
> >
> >   Trying to find a quick/slick/easily interpretable way to
> > collapse a data set.
> >

Another alternative for SQL fans is the sqldf package.  I used the MySQL driver
here since SQLite does not support standard deviation. 


sqldf("select BROOD, avg(TICKS) as 'TICKS.mean', stddev_samp(TICKS) as
'TICKS.sd', HEIGHT, YEAR, LOCATION
 from h
group by BROOD, HEIGHT, YEAR, LOCATION",  drv="MySQL")

  BROOD TICKS.mean TICKS.sd HEIGHT YEAR LOCATION
1   501          0     0.00    465   95       32
2   502          0       NA    472   95       36
3   503          1     1.73    475   95       37



Chris



More information about the R-help mailing list