[R] tapply with cbinded x

Wacek Kusnierczyk Waclaw.Marcin.Kusnierczyk at idi.ntnu.no
Tue Jun 16 13:32:29 CEST 2009


Stavros Macrakis wrote:
> On Tue, Jun 16, 2009 at 5:16 AM, Stefan Uhmann <stefan.uhmann at googlemail.com
>   
>> wrote:
>>     
>
>   
>> 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 tapply is defined for atomic vectors and not for data frames.  


that's not it -- the error message is about lengths.  cbind produces a
matrix, and tapply receives no data frame as input.  the input is of
correct type, but the lengths do not match -- the length of a 3x3 matrix
is 9.

> Why?
> I don't know.
>   

who does?  anyway, tapply does "work" for data frames, though it
contradicts the documentation.

vQ




More information about the R-help mailing list