[R] data.frame and formula classes of aggregate
dwinsemius at comcast.net
Mon Nov 29 15:49:38 CET 2010
On Nov 29, 2010, at 9:35 AM, David Freedman wrote:
> Hi - I apologize for the 2nd post, but I think my question from a
> few weeks
> ago may have been overlooked on a Friday afternoon.
> I might be missing something very obvious, but is it widely known
> that the
> aggregate function handles missing values differently depending if a
> frame or a formula is the first argument ?
I'm not sure if it is widely known, but it is certainly suggested by
the documentation for aggregate, since aggregate.data.frame has
different defaults than aggregate.formula. See the Usage section at
the very top of ?aggregate.
> For example,
> (d<- data.frame(sex=rep(0:1,each=3),
> x1<- aggregate(d, by = list(d$sex), FUN = mean);
> names(x1)[3:4]<- c('mean.dfcl.wt','mean.dfcl.ht')
> x2<- aggregate(cbind(wt,ht)~sex,FUN=mean,data=d);
> names(x2)[2:3]<- c('mean.formcl.wt','mean.formcl.ht')
> The output from the data.frame class has an NA if there are missing
> in the group for the variable with missing values. But, the formula
> output seems to delete the entire row (missing and non-missing
> values) if
> there are any NAs. Wouldn't one expect that the 2 forms (data frame
> formula) of aggregate would give the same result?
> thanks very much
> david freedman, atlanta
David Winsemius, MD
West Hartford, CT
More information about the R-help