[R] comparing class() -- R=NULL and S+=numeric

Peter Dalgaard BSA p.dalgaard at biostat.ku.dk
Tue Jan 14 22:41:06 CET 2003


"Rex_Bryan at urscorp.com" <RexBryan1 at attbi.com> writes:

> I'm just reading Modern Applied Statistics with S, 4th Ed., Venables and
> Ribley
> I'm typing in their examples in both R and S+.  I need insight in the
> difference
> in the class() statement shown in Chap. 2.  Example from book:
> 
> > names(powers.of.pi) <- -2:2
> > powers.of.pi
>        -2        -1         0         1         2
> 0.1013212 0.3183099 1.0000000 3.1415927 9.8696044
> 
> > class(powers.of.pi)
> R produces an answer:      NULL
> S+ produces the answer:  [1]  "numeric"
> 
> Any insight in understanding the difference?

I suspect that the answer is actually somewhere in MASS4, but I don't
have it at hand right now. 

Basically there have been two class systems in S(-PLUS), S3 and S4,
and R is (mostly) compatible with the former as represented by S-PLUS
3.x.

In S3, classes are really just text attributes, and if none is
present, the class is NULL. However, you can ask for data.class(x) and
get - essentially - its mode. E.g.,

> data.class(powers.of.pi)
[1] "numeric"

S4 classes introduce a stricter system where all objects have a class.
This is what is used in current versions of S-PLUS. In R, we have the
"methods" library, which changes the behaviour of R to be like S4:

> library(methods)
> class(powers.of.pi)
[1] "numeric"

-- 
   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)             FAX: (+45) 35327907




More information about the R-help mailing list