LoslegenKostenlos loslegen

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.frames. 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.

Diese Übung ist Teil des Kurses

Object-Oriented Programming with S3 and R6 in R

Kurs anzeigen

Anleitung zur Übung

  • 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.

Interaktive Übung

Versuche dich an dieser Übung, indem du diesen Beispielcode vervollständigst.

# 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
___
___
___
Code bearbeiten und ausführen