Have I been sleeping in class? rw1071 from CRAN, windows XP incidencia is made by a call to tapply> class(incidencia)[1] "array"> incidencia <- unclass(incidencia) > class(incidencia)[1] "array" Kjetil Halvorsen
> Have I been sleeping in class? > > rw1071 from CRAN, windows XP > > incidencia is made by a call to tapply > > > class(incidencia) > [1] "array" > > incidencia <- unclass(incidencia) > > class(incidencia) > [1] "array" > >`unclass' only removes the `class' attribute. However, arrays do not own one (see the details in ?class) but its class is defined implicitly: R> x <- array(rnorm(25), dim=c(5,5)) R> mode(x) [1] "numeric" R> class(x) [1] "matrix" R> attributes(x) $dim [1] 5 5 so nothing to remove here, in contrast to data.frames: R> x <- as.data.frame(matrix(rnorm(25), ncol=5)) R> attributes(x) $names [1] "V1" "V2" "V3" "V4" "V5" $row.names [1] "1" "2" "3" "4" "5" $class [1] "data.frame" R> unclass(x) $V1 [1] 1.4085747 0.2405006 0.7056615 -0.4747142 0.5846174 $V2 [1] 1.06994019 -0.26797839 -0.54426997 -3.34744272 0.05473644 $V3 [1] -0.75277466 0.05542274 -1.13621279 1.06582063 -1.04080035 $V4 [1] 0.3942039867 -0.2680249001 0.0002451802 -2.4269886369 0.8719898617 $V5 [1] 1.186167 0.374248 0.832176 1.030344 -1.606450 attr(,"row.names") [1] "1" "2" "3" "4" "5" Best Torsten> Kjetil Halvorsen > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help > >
kjetil brinchmann halvorsen wrote:> Have I been sleeping in class? > > rw1071 from CRAN, windows XP > > incidencia is made by a call to tapply > > >>class(incidencia) > > [1] "array" > >>incidencia <- unclass(incidencia) >>class(incidencia) > > [1] "array" > > > Kjetil Halvorsen > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-helpSee ?class: 'If the object does not have a class attribute, it has an implicit class, "matrix", "array" or the result of mode(x).' and ?unclass: 'unclass returns (a copy of) its argument with its class attribute removed.' Since the return of value "array" had to be introduced for S4 compatibility (all objects must have classes according to the green book - "array" is an implicit class), unclass() cannot remove that non-existant attribute. BTW: oldClass(incidencia) still returns NULL ..., doesn't it? Uwe Ligges
[BCC: to JMC]>>>>> "kjetil" == kjetil brinchmann halvorsen <kjetil at entelnet.bo> >>>>> on Sat, 16 Aug 2003 10:39:25 -0400 writes:kjetil> Have I been sleeping in class? yeah, probably the one on S4 classes/methods and the NEWS that the methods package is now always attached by default ;-) :-0) (yes, I'm joking). kjetil> ... >> class(incidencia) kjetil> [1] "array" >> incidencia <- unclass(incidencia); class(incidencia) kjetil> [1] "array" More specific: When the "methods" package is attached, every object has a class, and I'd expect unclass() to be no-op. HOWEVER, that's not quite true: 1) unclass() remains a no-op for "array" objects, even after detaching "methods". In R 1.6.1, we had > class(unclass(array(1:6, 1:3))) [1] "array" > detach("package:methods") > class(unclass(array(1:6, 1:3))) NULL > This is different in R 1.7.1 (the class remains "array"), and this is even true when "methods" was never attached (i.e. when starting R with no default packages). I'll let other R-corers explain why this might be desired. We might consider unclass(x) produce a warning whenever `x' has a class and that is not changed. 2) unclass() is not no-op for e.g. data frames; it returns a list, in both cases {"methods" attached, or not, the difference being that "list" is a class in the first case, but not the second}. Note that this is very necessary: If unclass() had become a no-op as soon as "methods" was attached, a lot of R code would break, notably also the case of unclass(<factor>) ! -- So, thank you, Kjetil, this was an interesting question and I've learned, too! Martin
I found an answer in "?unclass" and a minor difference between R 1.7.1 and S-Plus 6.1.2: First ?unclass in R 1.7.1 under Windows 2000: Many R objects have a `class' attribute, a character vector giving the names of the classes which the object ``inherits'' from. If the object does not have a class attribute, it has an implicit class, `"matrix"', `"array"' or the result of `mode(x)'. ... The function `class' prints the vector of names of classes an object inherits from. ... `unclass' returns (a copy of) its argument with its class attribute removed. Thus, "incidencia <- unclass(incidencia)" removed the class attribute from "incidencia". Then consistent with the documentation, "class(incidencia)" has an implicit class "array"; if it were not a matrix or array, then "class(incidencia)" would have returned the result of mode(x). Now, a trivial comparison between R 1.7.1 and S-Plus 6.1.2: ##R 1.7.1 > tst <- 2 > class(tst) [1] "numeric" > Tst <- unclass(tst) > class(Tst) [1] "numeric" > mode(Tst) [1] "numeric" ## Same thing in S-Plus 6.1.2: > tst <- 2 > class(tst) [1] "integer" > Tst <- unclass(tst) > class(Tst) [1] "integer" > mode(Tst) [1] "numeric" Note that "tst" and "Tst" have class and mode "numeric" in R 1.7.1 but class "integer" and mode "numeric" in S-Plus 6.1.2. hope this helps. spencer graves kjetil brinchmann halvorsen wrote:> Have I been sleeping in class? > > rw1071 from CRAN, windows XP > > incidencia is made by a call to tapply > > >>class(incidencia) > > [1] "array" > >>incidencia <- unclass(incidencia) >>class(incidencia) > > [1] "array" > > > Kjetil Halvorsen > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help