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 murders data frame. To extract variables from a data frame we use $
, referred to as the accessor.
# Load package and data
library(dslabs)
data(murders)
# To access the population variable from the murders dataset use this code:
p <- murders$population
# To determine the class of object `p` we use this code:
class(p)
Let’s try this ourselves!
Use the accessor $
to extract the state abbreviations (abb) and assign them to the object a
. What is the class of this object?
Replace the … by the correct code on lines 7 and 9.