Setzer.Woodrow@epamail.epa.gov
2002-Jan-16 16:18 UTC
[R] inconsistent(?) behavior of as.vector
According to the documentation for as.vector, it removes all attributes
from its argument ("the attributes of x are removed"). This does not
seem to be the case for a list with a dim attribute. Consider the
following code:
numarray <- vector("numeric",4)
dim(numarray) <- c(2,2)
dimnames(numarray) <-
list(c("A","B"),c("C","D"))
numarray[] <- 1:4
listarray <- vector("list",4)
dim(listarray) <- c(2,2)
dimnames(listarray) <-
list(c("A-","A+"),c("B-","B+"))
listarray[["A+","B-"]] <- A ~ 1
listarray[["A+","B+"]] <- A + B ~ 1
## You get the idea for the values of the elements of listarray
Now, what are the attributes in each case?> print(attributes(numarray))
$dim
[1] 2 2
$dimnames
$dimnames[[1]]
[1] "A" "B"
$dimnames[[2]]
[1] "C" "D"
> print(attributes(listarray))
$dim
[1] 2 2
$dimnames
$dimnames[[1]]
[1] "A-" "A+"
$dimnames[[2]]
[1] "B-" "B+"
So, both objects have the attributes I expected. However, applying as.vector
does not remove the atrributes from the object based on a list:
> print(str(as.vector(numarray)))
num [1:4] 1 2 3 4
NULL> print(str(as.vector(listarray)))
List of 4
$ : NULL
$ :Class 'formula' length 3 A ~ 1
.. ..- attr(*, ".Environment")=length 9 <environment>
$ : NULL
$ :Class 'formula' length 3 A + B ~ 1
.. ..- attr(*, ".Environment")=length 9 <environment>
- attr(*, "dim")= int [1:2] 2 2
- attr(*, "dimnames")=List of 2
..$ : chr [1:2] "A-" "A+"
..$ : chr [1:2] "B-" "B+"
NULL
I am working with version 1.4.0 (not patched) on Linux (RedHat 7.1 on
i686), but the same thing happens in 1.3.1 under Windows 98. Is this
intended behavior (in which case, the documentation for as.vector should
probably be changed), or a bug?
R. Woodrow Setzer, Jr. Phone:
(919) 541-0128
Experimental Toxicology Division Fax: (919)
541-5394
Pharmacokinetics Branch
NHEERL MD-74; US EPA; RTP, NC 27711
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !) To: r-help-request at
stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
