[R] Aggregating a List

Peter Dalgaard BSA p.dalgaard at biostat.ku.dk
Wed Nov 6 22:11:22 CET 2002


Paul Boutros <pcboutro at engmail.uwaterloo.ca> writes:

> Hi all,
> 
> There must be a really obvious R solution to this, but I can't figure out
> how to aggregate a list.  For instance, if I read.table the following from
> a file:
> 	Val1	Val2
> A	3	4
> A	5	6
> B	4	4
> 
> I would like to take the mean (or median) across any/all rows of type "A"
> to end up with the structure:
> 
> 	Val1	Val2
> A	4	5
> B	4	4
> 
> in this case.  How would I go about doign that w/o doing a loop or putting
> into a DB and using SQL to do this?
> 
> Thanks for any suggestions, or pointers at appropriate functions I should
> be reading the documentation on.

aggregate()

E.g., 

> g <- read.table(stdin(),header=T)
0: gr         Val1    Val2
1: A       3       4
2: A       5       6
3: B       4       4
4: 
> g
  gr Val1 Val2
1  A    3    4
2  A    5    6
3  B    4    4
> aggregate(g[-1],list(g$gr),mean)
  Group.1 Val1 Val2
1       A    4    5
2       B    4    4

or for that matter: aggregate(g[-1],g[1],mean)


-- 
   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)             FAX: (+45) 35327907
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list