On Sun, Sep 29, 2013 at 10:48 PM, john doe <anon.r.u...@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 ______________________________________________ R-help@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.