How does R Distinguish Variables?

Object-Oriented Programming with S3 and R6 in R

Richie Cotton

Data Evangelist at DataCamp

str()

str(sleep)
'data.frame':    20 obs. of  3 variables:
 $ extra: num  0.7 -1.6 -0.2 -1.2 -0.1 ...
 $ group: Factor w/ 2 levels "1","2": 1 1 1 1 1 ...
 $ ID   : Factor w/ 10 levels "1","2","3","4",..: 1 2..
class(sleep)
"data.frame"
Object-Oriented Programming with S3 and R6 in R

int_mat

(int_mat <- matrix(1:12, 3))
     [,1] [,2] [,3] [,4]
[1,]    1    4    7   10
[2,]    2    5    8   11
[3,]    3    6    9   12
class(int_mat)
"matrix"
typeof(int_mat)
"integer"
Object-Oriented Programming with S3 and R6 in R

num_mat

(num_mat <- matrix(rnorm(12), 3))
           [,1]       [,2]        [,3]       [,4]
[1,] -0.2911535 -0.1139933 -0.71290868  0.8640191
[2,] -2.2266419 -1.3604316 -1.90716974  0.4012884
[3,] -0.7504663 -1.2478873  0.01104117 -0.8127333
class(num_mat)
"matrix"
typeof(num_mat)
"double"
Object-Oriented Programming with S3 and R6 in R

 

 

 

mode()

storage.mode()

Object-Oriented Programming with S3 and R6 in R

Summary

  • class() is your first choice for determining the kind of variable
  • typeof() is also occasionally useful
  • mode() and storage.mode() are old functions; don't use them
Object-Oriented Programming with S3 and R6 in R

Let's practice!

Object-Oriented Programming with S3 and R6 in R

Preparing Video For Download...