In this module we have learned that every variable has a class. For example, the class can be a character, numeric or logical. The function class can be used to determine the class of an object.

Here we are going to determine the class of one of the variables in the Hitters data frame. To extract variables from a data frame we use $, referred to as the accessor.

Example

# Load package and data
library(ISLR2)
data(Hitters)

# To access the Salary variable from the Hitters dataset use this code:
p <- Hitters$Salary 

# To determine the class of object p we use this code:
class(p)

Let’s try this ourselves!

Use the accessor $ to extract the number of hits (Hits) and assign them to the object a. What is the class of this object?