[R] Understanding classes in R

Bert Gunter gunter.berton at gene.com
Mon Sep 30 01:10:28 CEST 2013


R-help is not the place for an extended tutorial the R class systems
(there are in fact at least two, S3 and S4, neither of which is like
C++ classes). Read the "R Language Definition" Manual that ships with
R, John Chambers's books (probably the latest for S4), or search the
web for tutorials (there are many).

Online Help is terse, but perhaps useful:

?UseMethod ## S3 classes

?setMethod
?Methods   ## for S4 classes

Cheers,
Bert

On Sun, Sep 29, 2013 at 2:48 PM, john doe <anon.r.user at gmail.com> wrote:
> I am having trouble understanding how classes in R work.  Here is a small
> reproducable example:
>
>> x=1
>> class(x)
> [1] "numeric"
>
> OK.  When a variable is a number, its class is "numeric".  Does R have
> multiple types for numbers, like C++ (eg integer, float, double).  If so,
> where can I see a list, and how does "numeric" fit into this system?
>
>> x=1:100
>> class(x)
> [1] "integer"
>
> Wait - I thought that I assigned x to be an array/vector of 100 integers
> (numerics).  Why is the class not "array" or "vector".  How is "integer"
> different than "numeric"?  Is there a "vector" or "array" class in R?  If
> so, why is this not that?
>
>> class(x[1])
> [1] "integer"
>
> This is even more confusing to me.  Because x[1] is 1.  And the class of
> that was "numeric" in my first example.  Why is it integer now?
>
>> x=1.5:100.5
>> class(x)
> [1] "numeric"
>
> Why is this class "numeric" when the class of 1:100 was integer?
>
> Thanks for your help.
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



-- 

Bert Gunter
Genentech Nonclinical Biostatistics

Internal Contact Info:
Phone: 467-7374
Website:
http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm



More information about the R-help mailing list