Assigning Classes

Object-Oriented Programming with S3 and R6 in R

Richie Cotton

Data Evangelist at DataCamp

(x <- rexp(10))
0.195051 2.191040 0.498703 0.976122 0.299001
0.105187 0.090073 2.328233 3.043201 2.129631
class(x) <- "random_numbers"
x
0.195051 2.191040 0.498703 0.976122 0.299001
0.105187 0.090073 2.328233 3.043201 2.129631
attr(,"class")
"random_numbers"
Object-Oriented Programming with S3 and R6 in R
class(x)
"random_numbers"
typeof(x)
"double"
Object-Oriented Programming with S3 and R6 in R
is.numeric(x)
TRUE
length(x)
10
mean(x)
1.1856
Object-Oriented Programming with S3 and R6 in R

Summary

  • You can override the class()
  • This won't break existing functionality
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...