15.1 The class attribute
In fact, everything in R is treated as an object. An object often has some attributes associated with it. These attributes can be seen as labeled values you can attach to an object. As an example, we can create a factor and inspect his attributes with the command attributes()
:
We can see that a factor object has two attributes: levels and class. The attribute class
now tells you from which “blueprint” (class) this object was built. You may realize now that all factors we’ve used so far were in fact objects of the class “factor”, a built-in class of R!
The attribute class
is where S3 objects come in. In fact, an S3 object is simply an object that has a class
attribute (and possibly other attributes to store data). You can see the class attribute specifically using the class()
command:
In fact, we can even change the class of an object over the class
attribute!