You've Already Been Working With Objects
In the Introduction to R course you already met several common R objects such as numeric
, logical
and character
vectors, as well as data.frame
s. One of the principles of OOP is that functions can behave differently for different kinds of object.
The summary()
(docs) function is a good example of this. Since different types of variable need to be summarized in different ways, the output that is displayed to you varies depending upon what you pass into it.
This exercise is part of the course
Object-Oriented Programming with S3 and R6 in R
Exercise instructions
- Run the code provided in the editor to create several objects of different types.
- Call
summary()
on each of these objects (one at a time), then examine the output and try to understand it.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Create these variables
a_numeric_vector <- rlnorm(50)
a_factor <- factor(
sample(c(LETTERS[1:5], NA), 50, replace = TRUE)
)
a_data_frame <- data.frame(
n = a_numeric_vector,
f = a_factor
)
a_linear_model <- lm(dist ~ speed, cars)
# Call summary() on the numeric vector
summary(a_numeric_vector)
# Do the same for the other three objects
___
___
___