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!


locked by whuber Feb 1 '17 at 19:18

This question exists because it has historical significance, but it is not considered a good, on-topic question for this site, so please do not use it as evidence that you can ask similar questions here. This question and its answers are frozen and cannot be changed. More info: help center.

Read more about locked posts here.


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.

  • 3
    $\begingroup$ Why there're so many points of view? I see no benefits but confusion. $\endgroup$ – smwikipedia Oct 10 '13 at 9:29
  • 1
    $\begingroup$ @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. $\endgroup$ – Gavin Simpson Oct 10 '13 at 14:16
  • $\begingroup$ @smwikipedia because that's just how R is. For the same reason we have <- and = for variable assignment. $\endgroup$ – Andre Terra Oct 15 '15 at 17:56

From: https://www.mail-archive.com/r-help@r-project.org/msg17169.html :

'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.


The main difference between class and typeof is that the first can be defined by the user, but the type cannot. For example, define a list

> x<-list("a",c(1,2))

> # x is a list
> class(x)
[1] "list"
> # class can be user defined
> class(x)<-"newclass"
> class(x)
[1] "newclass"

> typeof(x)
[1] "list"
# you cannot assign a different type using typeof()
> typeof(x)<-"newclass"
Error in typeof(x) <- "newclass" : could not find function "typeof<-" 

To give a certain class name to a user defined object is very useful to write programs. It allows to tag user defined objects in a similar way to what happens in object-oriented programming languages.

  • $\begingroup$ According to the R documentation for typeof and class, the assertion about "main difference" is incorrect. class is an attribute of an object that can be assigned regardless of its internal storage mode, while "typeof determines the (R internal) type or storage mode of any object." One describes a logical characteristic while the other is a physical characteristic of an object. $\endgroup$ – whuber Feb 1 '17 at 19:21

Not the answer you're looking for? Browse other questions tagged or ask your own question.