[R] more on apply on data frame
Göran Broström
gb at stat.umu.se
Sat Aug 21 16:48:53 CEST 2004
On Fri, Aug 20, 2004 at 11:40:16PM -0500, Laura Holt wrote:
> Hi R People:
>
> Several of you pointed out that using "tapply" on a data frame will work on
> the iris data frame.
>
> I'm still having a problem.
>
> The iris data frame has 150 rows, 5 variables. The first 4 are numeric,
> while the last is a factor, which has the Species names.
>
> I can use tapply for 1 variable at a time:
> >tapply(iris[,1],iris[,5],mean)
> setosa versicolor virginica
> 5.006 5.936 6.588
> >
> but if I try to use this for all of the first 4, I get an error:
> >tapply(iris[,1:4],iris[,5],mean)
> Error in tapply(iris[, 1:4], iris[, 5], mean) :
> arguments must have same length
> >
> Any ideas of what I'm doing wrong, please?
You are not reading the help page:
Usage:
tapply(X, INDEX, FUN = NULL, ..., simplify = TRUE)
Arguments:
X: an atomic object, typically a vector.
iris[, 1:4] is a data frame of length 4; iris[, 5] is a vector of length 150.
You probably need to loop over the four first columns and apply tapply(!)
four times, but I'm sure there is a smarter way. Others will tell you.
Göran
--
Göran Broström tel: +46 90 786 5223
Department of Statistics fax: +46 90 786 6614
Umeå University http://www.stat.umu.se/egna/gb/
SE-90187 Umeå, Sweden e-mail: gb at stat.umu.se
More information about the R-help
mailing list