Hi, I'm having some problems with the aggregate() function in the {stats} package, and the documentation doesn't address them. 1) Why would the first line work, but the second not? According to the help file, it accepts a "data=" argument.> with(tsrc, aggregate(x=DistRatio, by=list(Condition), FUN=mean))Group.1 x 1 Congruent 1.741789 2 Mismatch 1.771425> aggregate(x=DistRatio, by=list(Condition), data=tsrc, FUN=mean)Error in aggregate(x = DistRatio, by = list(Condition), data = tsrc, FUN = mean) : object 'DistRatio' not found 2) The subset argument also does not appear to work (perhaps this is the same issue?):> with(tsrc, aggregate(x=DistRatio, by=list(Condition), subset=Drop!="Yes", FUN=mean))Group.1 x 1 Congruent 1.741789 2 Mismatch 1.771425> with(tsrc[tsrc$Drop!="Yes",], aggregate(x=DistRatio, by=list(Condition), FUN=mean))Group.1 x 1 Congruent 1.700215 2 Mismatch 1.859795 So, am I doing something wrong or is this function just not working as advertised? Thanks, Jon -- Jon Zadra Department of Psychology University of Virginia P.O. Box 400400 Charlottesville VA 22904 (434) 982-4744 email: zadra at virginia.edu <http://www.google.com/calendar/embed?src=jzadra%40gmail.com>
Marc Schwartz
2011-Sep-15 21:50 UTC
[R] Problems with aggregate() function in stats package
On Sep 15, 2011, at 4:07 PM, Jon Zadra wrote:> Hi, > > I'm having some problems with the aggregate() function in the {stats} package, and the documentation doesn't address them. > > 1) Why would the first line work, but the second not? According to the help file, it accepts a "data=" argument. > >> with(tsrc, aggregate(x=DistRatio, by=list(Condition), FUN=mean)) > Group.1 x > 1 Congruent 1.741789 > 2 Mismatch 1.771425 > >> aggregate(x=DistRatio, by=list(Condition), data=tsrc, FUN=mean) > Error in aggregate(x = DistRatio, by = list(Condition), data = tsrc, FUN = mean) : > object 'DistRatio' not found > > > 2) The subset argument also does not appear to work (perhaps this is the same issue?): > >> with(tsrc, aggregate(x=DistRatio, by=list(Condition), subset=Drop!="Yes", FUN=mean)) > Group.1 x > 1 Congruent 1.741789 > 2 Mismatch 1.771425 > >> with(tsrc[tsrc$Drop!="Yes",], aggregate(x=DistRatio, by=list(Condition), FUN=mean)) > Group.1 x > 1 Congruent 1.700215 > 2 Mismatch 1.859795 > > > So, am I doing something wrong or is this function just not working as advertised? > > Thanks, > > JonYou are combining syntax from the data frame method for aggregate() with arguments (data and subset) that are only available in the formula method. Be sure to read the help page for the function and note at the top of the page, the different syntax for each method. Of course, note the examples as well. If you want to use the formula method with 'data' and 'subset' you would need to use something like: aggregate(DistRatio ~ Condition, data = tsrc, FUN = mean) or: aggregate(DistRatio ~ Condition, data = tsrc, subset = Drop != "Yes", FUN = mean) HTH, Marc Schwartz