[R] By processing on two variables at once?

David Winsemius dwinsemius at comcast.net
Thu Nov 12 04:19:13 CET 2009


On Nov 11, 2009, at 8:51 PM, zwarren wrote:

>
> Hello!
>
> I'm trying to runs stats on two vars at a time in a big data frame.   
> I knew
> how to do this in SAS many years ago, but have half-forgotten that  
> as well!
>
> I need, for instance, mean(value) by x-y combination:
> x   y   z   value
> 1   1   1    10
> 1   1   2    20
> 1   2   1    30
>
> with results:
> x   y   mean(value)
> 1   1    15
> 1   2    30

It's generally a bad idea to incorporate "(" in variable names, but  
it's possible:

 > aggregate(dat$value, list(dat$x, dat$y), mean)
   Group.1 Group.2  x
1       1       1 15
2       1       2 30
 > newdat <-aggregate(dat$value, list(dat$x, dat$y), mean)
 > names(newdat) <- c("x","y",bquote("mean(value)") )
 > newdat
   x y mean(value)
1 1 1          15
2 1 2          30

>
> Any help?
>
> Thanks,
>
> ~Zack
> -- 
> View this message in context: http://old.nabble.com/By-processing-on-two-variables-at-once--tp26312115p26312115.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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.

David Winsemius, MD
Heritage Laboratories
West Hartford, CT




More information about the R-help mailing list