- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What is the difference between class and typeof function in R?
The class function in R helps us to understand the type of object, for example the output of class for a data frame is integer and the typeof of the same object is list because data frames are stored as list in the memory but they are represented as a data frame. Check out the below examples with multiple type of objects to understand the differences.
Example1
x1<-rpois(20,2) typeof(x1)
Output
[1] "integer"
class(x1)
[1] "integer"
df1<-data.frame(x1) df1
x1 1 1 2 4 3 1 4 0 5 2 6 2 7 4 8 2 9 3 10 4 11 1 12 4 13 0 14 5 15 2 16 2 17 0 18 4 19 3 20 3
typeof(df1)
[1] "list"
class(x1)
[1] "integer"
Example2
M<-matrix(rnorm(40),ncol=2) M
Output
[,1] [,2] [1,] 2.02437789 -0.9161853 [2,] -0.60108978 -0.8972007 [3,] 1.27916953 0.1017923 [4,] 1.06998017 1.4839931 [5,] 0.22298522 0.6160919 [6,] -0.29346341 -0.3975116 [7,] 2.07012097 -0.7900820 [8,] 0.36719470 -0.1100298 [9,] -0.69522122 -1.9198172 [10,] 2.07822428 0.2517532 [11,] -1.56267422 1.8295022 [12,] -1.07488221 1.2666054 [13,] -0.79381494 -1.0993693 [14,] -0.16027224 -1.1814177 [15,] 0.67561791 0.7309281 [16,] -1.40912018 -0.3307749 [17,] -0.77769513 0.5527600 [18,] 0.47050704 0.1075593 [19,] -0.46616151 -0.5079660 [20,] -0.01944371 0.1553333
typeof(M)
[1] "double"
class(M)
[1] "matrix" "array"
Advertisements