[Rd] drop() and "["(), and apply()

Peter Dalgaard p.dalgaard at biostat.ku.dk
Sat Oct 15 22:19:49 CEST 2005


Dan Davison <davison at uchicago.edu> writes:

> I have two queries about the behaviour of drop() and apply() regarding the 
> dimnames and names(dimnames) of the answer. I would appreciate any 
> comments on this behaviour. I will submit any of this as a bug report if I 
> am encouraged to do so.
> 
> The first query, concerning drop(), seems to me to be a bug in ?drop, and 
> possibly also in the function.
> 
> ?drop:
> [...] Any accompanying 'dimnames' attribute is adjusted and returned with 
> 'x'.
> 
> > x <- array(dim=c(3,2,1), dimnames=list(A=NULL, B=NULL, C=NULL))
> 
> > attributes(drop(x))
> $dim
> [1] 3 2
> 
> > attributes(x[,,])
> $dim
> [1] 3 2
> 
> I expected a dimnames attribute equivalent to list(A=NULL, B=NULL). I 
> think ?drop is misleading. Is the behaviour intended?

I think the logic is that if all dimnames are NULL, they are
discarded from the result. This probably predates the use of named
dimnames. Named NULL dimnames were not anticipated then, nor since...
 
> ###########################################
> 
> I am less confident that the second query, concerning apply(), is a bug in 
> anything. But I would appreciate an explanation of the behaviour.
> 
> > x <- array(dim=c(3,2), dimnames=list(A=1:3, B=1:2))
> 
> > apply(x, 1, function(row) row)
>     A
>       1  2  3
>    1 NA NA NA
>    2 NA NA NA
> 
> > attributes(apply(x, 1, function(row) row))
> $dim
> [1] 2 3
> 
> $dimnames
> $dimnames[[1]]
> [1] "1" "2"
> 
> $dimnames$A
> [1] "1" "2" "3"
> 
> What has happened to the name of the first element of the list of dimnames 
> of the answer? It seems arguable that if the rows of the answer are 
> allowed to keep their names, then the name of that dimension should also 
> be preserved. Should the behaviour be better documented in ?apply (which 
> only discusses the effect on the dim attribute AFAICS).
> 
> 
> Thanks for any comments. Please let me know if I should submit any bug 
> reports concerning any of the above.

I think this happens because vectors cannot have named dimnames.
Basically this happens because there's no obvious place to put it:

> dput(c(a=1))
structure(1, .Names = "a")

i.e., the names are in a vector, not a list. This is a bit of a
bother (and forces the use of 1D arrays in some places, which in turn
confuses code that expects a vector), but implementing code that
carries an extra "name-name" attribute through all possible
calculations have been considered unattractive...

So, the function inside your apply()  construct returns a vector with
the right names, but loses the name of the names, at which point there
is no way to retrieve them.

-- 
   O__  ---- Peter Dalgaard             Øster Farimagsgade 5, Entr.B
  c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
 (*) \(*) -- University of Copenhagen   Denmark          Ph:  (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)                  FAX: (+45) 35327907



More information about the R-devel mailing list