[R] Table rearranging

William Dunlap wdunlap at tibco.com
Tue Feb 7 01:43:25 CET 2012


Install and load the "plyr" package and try  something like:

> ddply(d, .(date, color), summarize,
+       meanOpen=mean(measurement[door=="open"]), nOpen=sum(door=="open"),
+       meanClosed=mean(measurement[door=="closed"]), nClosed=sum(door=="closed"))
  date  color  meanOpen nOpen meanClosed nClosed
1  420    red 0.9741929     1        NaN       0
2  513    red 0.9352938     1  0.9620535       1
3  917 yellow       NaN     0  0.9941022       1
4 1230   blue 0.9639099     1  0.9893108       1
5 1230  green 0.9765203     1        NaN       0

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com 

> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Jeffrey Joh
> Sent: Monday, February 06, 2012 4:28 PM
> To: r-help at r-project.org
> Subject: [R] Table rearranging
> 
> 
> I have a table that looks like this:
> 
> measurement    date    door    color
> 0.93529385    513    open    red
> 0.97419293    420    open    red
> 0.962053514    513    closed    red
> 0.963909937    1230    open    blue
> 0.97652034    1230    open    green
> 0.989310795    1230    closed    blue
> 0.9941022    917    closed    yellow
> 
> I would like to create a table that has: Open measurement, Closed measurement, date, color.  For every
> date/color combination, there should be two columns to represent the door open/closed measurement.
> 
> If there are multiple datapoints with a given door/date/color combination, then they should be
> averaged.
> I would also like to make two columns to represent the number of
> datapoints that were averaged in determining the open/closed
> measurements.
> 
> Jeffrey
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list