[R] aggregate.data.frame with NAs and different types

Spencer Graves spencer.graves at structuremonitoring.com
Sun May 12 22:54:27 CEST 2013


       Do you have suggestions for how to aggregate a data.frame using 
different functions on different columns?

       Consider the following example:

df2aggregate <- data.frame(id=rep(letters[1:4], each=2),
                            x =c(1:6, NA, NA),
                            y =c(NA, 1:6, NA),
                            a =c(NA, NA, LETTERS[1:6]),

# Desired output:

ag1.2 <- data.frame(id=letters[1:4],
                     x =c(3, 7, 11, NA),
                     y =c(NA, 2.5, 4.5, NA),
                     a =c(NA, 'A', 'C', 'E'),

       I'm thinking of writing a function Aggregate(x, by, FUN, ...), 
where x = data.frame, by = vector of names of columns of x, and FUN = 
function that would accept as input a data.frame subset of x and would 
return a data.frame FUNout, which would be combined using cbind(x[, by], 
FUNout), then rbind over all such subset data.frames.  However, before I 
write this, I'd like to make sure it doesn't already exist.  My current 
plan is to add it to the Ecdat package.

       Suggestions?  Should I study "plyr"?  fortune(298) ;-)


p.s.  library(sos); findFn('aggregate.data.frame') returned 4 matches, 
none of which seemed to solve this problem. findFn('aggregate 
data.frame') returned 133 matches in 71 package. findFn('aggregate') 
returned 734 matches in 282 packages.  I failed to find anything useful 
in the latter two and with other attempts using RSiteSearch, except for 
a reference to plyr.

Spencer Graves, PE, PhD
President and Chief Technology Officer
Structure Inspection and Monitoring, Inc.
751 Emerson Ct.
San José, CA 95126
ph:  408-655-4567
web:  www.structuremonitoring.com

More information about the R-help mailing list