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 murders data frame. To extract variables from a data frame we use $, referred to as the accessor.

Example

# Load package and data:
library(dslabs)
data(murders)

# Access the population variable from the murders dataset: 
p <- murders$population 

# Determine the class of object `p`:
class(p)

Let’s try this ourselves!

Exercise

  1. Use the accessor $ to extract the state abbreviations (abb) and assign them to the object named abbreviations.

  2. Determine the class of the object abbreviations. Store your result in an object named abbreviations_class

  3. Give another example you can think of with the same datatype as the state abbreviations variable and assign it to the object named same_datatype.


NOTE We have already loaded in the murders dataframe for you.