Hi r-help-boun...@r-project.org napsal dne 16.06.2009 12:45:04:
> Stefan Uhmann wrote: > > Dear List, > > > > why does this not work? > > > > df <- data.frame(var1 = c(3,2,1), var2 = c(6,5,4), var3 = c(9,8,7), > > fac = c('A', 'A', 'B')) > > tapply(cbind(df$var1, df$var2, df$var3), df$fac, mean) > > because > > length(cbind(df$var1, df$var2, df$var3)) > # 9 This is the problem with cbinding anything. You will get matrix which is basically a vector with dimensions > dim(cbind(df$var1, df$var2, df$var3)) [1] 3 3 > > length(df$fac) > # 3 > > and that's enough for it not to work, as far as i understand what > ?tapply says. > > here's another question: why this *does* work (or "work"): > > d = data.frame(a=1:3, b=1:3, c=1:3) > f = factor(1:3) > > tapply(d, f, c) > # no issues Tapply does not check the arguments, it just check if d and f has the same length, which in your case is true. > length(d)==3 [1] TRUE > and the aggregation function itself is lapply(split(d, f), c) Which is in your case also valid although not very meaningful. Maybe the help page shall state that with data.frame as first argument you can get unexpected results. However I would expect that help page shows you recommended way of doing things not to warn you about any possible combination of actions. Regards Petr > > although > > is.atomic(d) > # FALSE > > and ?tapply says: > > " X: an atomic object, typically a vector." > > note the 'atomic'. > > vQ > > ______________________________________________ > R-help@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. ______________________________________________ R-help@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.