[R] Using split and sapply to return entire lines

Todd A. Gibson tgibson at augustcouncil.com
Tue Nov 8 21:20:17 CET 2005


Hello,
I have a data manipulation problem that I can easily resolve by using
perl or python to pre-process the data, but I would prefer to do it
directly in R.

Given, for example:

  month length ratio monthly1 monthly2
1 Jan   23     0.1   9        6
2 Jan   45     0.2   9        6
3 Jan   16     0.3   9        6
4 Feb   14     0.2   1        9
5 Mar   98     0.4   2        2
6 Mar   02     0.6   2        2

(FWIW, monthly1 and monthly2 are unchanged for each month)

I understand how to do aggregations on single fields using split and
sapply, but how can I get entire lines.  For example, For the maximum
of data$length grouped by data$month I would like to get back some
form of:

2 Jan 45 0.2 9 6
4 Feb 14 0.2 1 9
5 Mar 98 0.4 2 2

For mean, I would like to average all columns:

Jan 28 0.2 9 6
Feb 14 0.2 1 9
Mar 50 0.5 2 2

Thank you,
-TAG
Todd A. Gibson




More information about the R-help mailing list