[R] Aggregate with functions using multiple arguments

From: Matt Pocernich <pocernic_at_rap.ucar.edu>
Date: Fri, 21 Mar 2008 15:15:11 -0600


 Hello,  

 I would like to use aggregate with a function that  requires several argument that are columns of data.  

 As a simple example, suppose I have data in the following dataframe and I  would like to summarize the difference between columns obs and frc for each  site. How would I do this? (In reality, the function is slightly more  complicated, but still requires several columns of data.)    

> DAT <- data.frame(site = rep(c("A","B"), 10), obs = rnorm(20), frc =

    rnorm(20))  

> DAT

    site         obs         frc
 1     A  1.27451057 -1.68995017
 2     B  1.43942253  0.41672963
 3     A -0.10875319  0.77108721
 4     B -0.63198144 -0.21772356
 5     A -0.42084163  0.50997647

 ....  

 I have tried variations on the following syntax with no success.  

> F <- function(sub){

  mean(sub[,"obs"] - sub[,"frc"] )
 }  

> aggregate(DAT[,c("obs", "frc")], by = list(DAT$site), F)
   

 I have had partial success with the by command, but the by-class object is  awkward and I would like to use the aggregate command to be consistent with  other functions.    

 Thanks,  

 Matt        

 --
 Matt Pocernich
 National Center for Atmospheric Research  Research Applications Laboratory
 (303) 497-8312

-- 
Matt Pocernich
National Center for Atmospheric Research
Research Applications Laboratory
(303) 497-8312

______________________________________________
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.
Received on Fri 21 Mar 2008 - 21:19:35 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Sat 22 Mar 2008 - 15:30:23 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive