[R] Why can't I "unclass" an array?

Martin Maechler maechler at stat.math.ethz.ch
Fri Dec 21 15:58:34 CET 2012


[ Diverted from  R-devel : ]

>>>>> Terry Therneau <therneau at mayo.edu>
>>>>>     on Fri, 21 Dec 2012 08:05:21 -0600 writes:

    > In a real example I was trying to remove the class from the result of table, just because 
    > it was to be used as a building block for other things and a simple integer vector seemed 
    > likely to be most efficient.
    > I'm puzzled as to why unclass doesn't work.

It does work, exactly as intended
(but indeed not as expected by you):

?unclass    says

     ‘unclass’ returns (a copy of) its argument with its class
     attribute removed. 

Note *attribute*.
In R (and I think newer versions of S, too),
class(x) *always* returns something reasonable, so there is
always an implicit class, e.g. "integer" as see below.

"matrix" and "array" are of a similar category in this respect,
their class is no attribute :

> attributes(array(1,1))
$dim
[1] 1

> attributes(matrix(1,1))
$dim
[1] 1 1

> m <- matrix(1,1); identical(m, unclass(m))
[1] TRUE
> a <- array(1,1); identical(a, unclass(a))
[1] TRUE
> 

Martin Maechler, ETH Zurich


    >> zed <- table(1:5)
    >> class(zed)
    > [1] "table"
    >> class(unclass(zed))
    > [1] "array"
    >> class(unclass(unclass(unclass(zed))))
    > [1] "array"

    >> class(as.vector(zed))
    > [1] "integer"
    >> sessionInfo()
    > R Under development (unstable) (2012-11-28 r61176)
    > Platform: i686-pc-linux-gnu (32-bit)

    > locale:
    > [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C
    > [3] LC_TIME=en_US.UTF-8        LC_COLLATE=C
    > [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8
    > [7] LC_PAPER=C                 LC_NAME=C
    > [9] LC_ADDRESS=C               LC_TELEPHONE=C
    > [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C

    > attached base packages:
    > [1] stats     graphics  grDevices utils     datasets  methods   base

    > ______________________________________________
    > R-devel at r-project.org mailing list
    > https://stat.ethz.ch/mailman/listinfo/r-devel



More information about the R-help mailing list