Take the 2-minute tour ×
Cross Validated is a question and answer site for people interested in statistics, machine learning, data analysis, data mining, and data visualization. It's 100% free, no registration required.

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!

share|improve this question
2  
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
add comment

1 Answer

up vote 15 down vote accepted

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.

share|improve this answer
    
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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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