[R] Aggregate with functions using multiple arguments

Matt Pocernich pocernic at rap.ucar.edu
Fri Mar 21 22:15:11 CET 2008


 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



More information about the R-help mailing list