[R] tapply with cbinded x

Wacek Kusnierczyk Waclaw.Marcin.Kusnierczyk at idi.ntnu.no
Tue Jun 16 12:45:04 CEST 2009


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

    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

although

    is.atomic(d)
    # FALSE

and ?tapply says:

    " X: an atomic object, typically a vector."

note the 'atomic'.

vQ




More information about the R-help mailing list