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 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
On Sep 29, 2013, at 2:48 PM, john doe 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".Well, it is a vector. But it's not an array because it has no 'dim' attribute. The ":" operator returns integer sequences. ?":"> How is "integer" > different than "numeric"? Is there a "vector" or "array" class in R?Yes. ?array ?vector ?is.vector> If > so, why is this not that? > >> class(x[1]) > [1] "integer" > > This is even more confusing to me. Because x[1] is 1.Look at: x=1L class(x)> 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? >Intergers are stored as 4-byte per item with a bit of overhead, which numerics or doubles are stored with 8 bytes:> object.size(1:100000)400040 bytes> object.size( (1:100000)+.5 )800040 bytes As you can see it is very easy to corce an integer to numeric.> Thanks for your help. > > [[alternative HTML version deleted]]And do learn to post in plain text.> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.-- David Winsemius Alameda, CA, USA
Hi JD, On Sun, Sep 29, 2013 at 5: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).Yes, but the class is not the type:> x <- 1:10 > class(x)[1] "integer"> typeof(x)[1] "integer"> class(x) <- "foo" > class(x)[1] "foo"> typeof(x)[1] "integer" If so,> where can I see a list, and how does "numeric" fit into this system?A list of what? For a list of storage modes see ?typof. For classes there cannot be any such list, as you can create a new class as easily as class(x) <- "aNewClassThatNeverExistedBefore"> >> 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?See http://cran.r-project.org/doc/manuals/r-release/R-intro.html#Objects (And while you are there it wouldn't be a bad idea to read the rest of manual as well).> >> 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?Presumably because '[' turned it into one. help("[") says that the return value is "typically an array-like R object of a similar class as ?x?."> >> x=1.5:100.5 >> class(x) > [1] "numeric" > > Why is this class "numeric" when the class of 1:100 was integer?Because 1.5 is not an integer. See the Value section of help(":")> > 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.
On Sun, Sep 29, 2013 at 10: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?In most programming languages scalars and vectors (aka 1-d arrays) are completely different things. However in R a scalar is the same thing as a length-1 vector. Don't think of x=1 and x=1:100 as the first creating a scalar and the second creating a vector containing 100 scalar values. The first creates a vector containing 1 scalar value, and the second creates a vector conatining 100 scalar values. You can't really get scalar values, they'll always effectively be in a vector of length 1. So the return value of class here is actually short for 'vector of numeric' or 'vector of integer' - even when the length is 1 - and you can think of those as the basic numeric classes. There is no 'scalar numeric' class, all is vectors. is.vector(1) is TRUE. There isn't even an is.scalar function. None of that is true for 2-d arrays, aka matrices, where class(m) is always "matrix" whether its a matrix of characters or numbers. You have to look at the mode(m) or typeof(m) (or storage.mode(m)) to figure out what kind of thing a matrix 'm' contains. A 'list' is a bit more like some of the generic container classes that you find in other programming langages. Its elements can be anything but its class is always 'list'. Use it when you want a vector (in the general sense of 'vector') of non-scalar values, eg L list(c(1,2,3),1,c(99,120),c("foo","bar","baz")) Confused? Well, just forget everything you learned about classes in your Comp Sci lessons and get ready to learn R's two incompatible OO-programming systems (S3 and S4 classes), or four if you want to look at even more OO systems people have implemented as add-on packages.... Barry