Difference between mode and class

‘mode’ is a mutually exclusive classification of objects according to their basic structure. The ‘atomic’ modes are numeric, complex, character ,logical, lists .
‘class’ is a property assigned to an object that determines how to operate with it. 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’.

Que: Check the class and mode of a matrix

v<-cbind(c(1,2,3),c(4,5,6))
v
##      [,1] [,2]
## [1,]    1    4
## [2,]    2    5
## [3,]    3    6

Let’s check the class now.

class(v)
## [1] "matrix"

Let’s check the mode now.

mode(v)
## [1] "numeric"

Que: Check the class and mode of a dataframe

name<-c("Ram","Abilash","vishnusri","praneet")
age<-c(23,22,21,25)
height<-c(175,163,160,170)
weight<-c(55,65,70,67)

bmi<-data.frame(Name_of_student=name,Age_of_student=age,Height_of_student=height,weight_of_student=weight)

print(bmi)
##   Name_of_student Age_of_student Height_of_student weight_of_student
## 1             Ram             23               175                55
## 2         Abilash             22               163                65
## 3       vishnusri             21               160                70
## 4         praneet             25               170                67

Now, lets’s check the class of bmi

class(bmi)
## [1] "data.frame"

Now, Let’s check the mode of bmi

mode(bmi)
## [1] "list"

Que: Check the class and mode of a list

L<-list(name="Pradeep",salary=50000,ratified=T,Subjects_taught=c("C Language","SRP","Data warehousing and Mining"))
L
## $name
## [1] "Pradeep"
## 
## $salary
## [1] 50000
## 
## $ratified
## [1] TRUE
## 
## $Subjects_taught
## [1] "C Language"                  "SRP"                        
## [3] "Data warehousing and Mining"

Now, let’s check the class(L)

class(L)
## [1] "list"

Now, let’s check the mode(L)

mode(L)
## [1] "list"

Que: Check the class and mode of a factor

x<-c("a","b","b","c","d")
xf<-factor(x)
xf
## [1] a b b c d
## Levels: a b c d

Now, let’s check the class(xf)

class(xf)
## [1] "factor"

Now, let’s check the mode(xf)

mode(xf) # Data is stored in terms of levels in case of factors
## [1] "numeric"