[R] arrays emerging from tapply
Peter Dalgaard
p.dalgaard at biostat.ku.dk
Thu Jan 6 17:32:30 CET 2005
"BXC (Bendix Carstensen)" <bxc at steno.dk> writes:
> The code below illustrates some points about results from tapply that
> I find strange. I wonder if they are intended and if so why it is so.
>
> 1) When you make a table the dimnames is a *named* list, tapply
> returns an unnamed list.
That depends on how you call it:
> tapply(airquality$Ozone,list(m=airquality$Month), mean,na.rm=T)
m
5 6 7 8 9
23.61538 29.44444 59.11538 59.96154 31.44828
The table() function does a bit of extra user-friendliness using the
code controlled by deparse.level, that's all. If we had a named.list()
(or so) function that extended list() with similar functionality,
tapply calls could be made almost as user friendly.
> 2) data.frame behaves differently on an array and a table. Is this
> an intended feature?
Yes (of course it is. What did you expect? Someone to have coded this
in his sleep?). You do want to be able to convert a matrix to a data
frame in the obvious way (d[i,j] <- m[i,j]), but for a table it is
more likely that you want to convert to the format suitable for a
Poisson glm analysis.
> 3) For tables class(TAB) and attr(TAB,"class") both return "table",
> but class(ARR) returns "array" whereas attr(TAB,"class") returns
> NULL.
Yes, that's a design quirk. In S3, objects only had a class if
explicitly specified via a class attribute, but nowadays all objects
have a class, so that methods can be dispatched on "numeric", etc.
The remaining issue (even if you don't mention it) is why tapply does
not return a table. This could arguably be a better choice, but I
suspect that some code depends on using tapply in ways that would be
incompatible. The change to returning a 1D array instead of a vector
certainly caused some trouble.
--
O__ ---- Peter Dalgaard Blegdamsvej 3
c/ /'_ --- Dept. of Biostatistics 2200 Cph. N
(*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
More information about the R-help
mailing list