# Mode, Class and Type of R objects

I was wondering what are the differences between Mode, Class and Type of R objects? Type of a R object can be obtained by typeof() function, mode by mode(), and class by class().

Also any other similar functions and concepts that I missed?

Thanks and regards!

-
str(variable) is your best friend. –  Brandon Bertelsen Sep 30 '10 at 18:21
See also R: Types and classes of variables question on Stack Overflow. –  Leo Nov 4 '12 at 17:16

The class() is used to define/identify what "type" an object is from the point of view of object-oriented programming in R. So for

> x <- 1:3
> class(x)
[1] "integer"


any generic function that has an "integer" method will be used.

typeof() gives the "type" of object from R's point of view, whilst mode() gives the "type" of object from the point of view of Becker, Chambers & Wilks (1988). The latter may be more compatible with other S implementations according to the R Language Definition manual.

I'd probably err on the side of using typeof() in most cases unless it was for passing R objects to compiled code, where storage.mode() will be useful.

This is usefully discussed in the R Language Definition as linked to above.

-
Why there're so many points of view? I see no benefits but confusion. –  smwikipedia Oct 10 '13 at 9:29
@smwikipedia because R has a heritage that includes S-Plus and S and that latter language evolved over time. Also, there is a need to differentiate between the objects users can create and the base object types. –  Gavin Simpson Oct 10 '13 at 14:16

'mode' is a mutually exclusive classification of objects according to their basic structure. The 'atomic' modes are numeric, complex, character and logical. Recursive objects have modes such as 'list' or 'function' or a few others. An object has one and only one mode.

'class' is a property assigned to an object that determines how generic functions operate with it. It is not a mutually exclusive classification. If an object has no specific class assigned to it, such as a simple numeric vector, it's class is usually the same as its mode, by convention.

Changing the mode of an object is often called 'coercion'. The mode of an object can change without necessarily changing the class.

-