From davison at uchicago.edu  Sat Oct 15 21:46:33 2005
From: davison at uchicago.edu (Dan Davison)
Date: Sat, 15 Oct 2005 14:46:33 -0500 (CDT)
Subject: [Rd] drop() and "["(), and apply()
Message-ID: <Pine.GSO.4.62.0510111523350.23321@harper.uchicago.edu>

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 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.

Dan

> version
          _
platform i386-pc-linux-gnu
arch     i386
os       linux-gnu
system   i386, linux-gnu
status
major    2
minor    2.0
year     2005
month    10
day      06
svn rev  35749
language R

Debian linux
dan at dd:~$ uname -a
Linux dd.princeton.edu 2.6.6 #1 SMP Fri Jul 16 13:23:04 EDT 2004 i686 
GNU/Linux



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