Created
March 30, 2023 13:30
-
-
Save graebnerc/f5d8203ec2c4973763b494628918ebe0 to your computer and use it in GitHub Desktop.
Lecture notes and solutions to the in-class exercises of session 4 in the spring semester 2023.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Lecture notes and solutions to the in-class exercises of session 4 in the spring semester 2023. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(tibble) | |
# Task 1------------------------------- | |
# Create a factor with the levels "still", "medium", "sparkling", | |
# and arbitrary instances of the three levels | |
# Get the relative frequencies for "medium" of this factor | |
# Task 2------------------------------- | |
# Create a data frame with two columns, one called "nb" containing the | |
# numbers 1 to 5 as double, the other called "char" containing the | |
# numbers 6 to 10 as character | |
# Transform this data frame into a tibble! | |
# Extract the second column of this tibble such that you have a vector |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Advanced object types================ | |
library(tibble) | |
library(dplyr) | |
## Factors----------------------------- | |
# Define a factor: | |
f_1 <- factor( | |
x = c(rep("F", 4), | |
rep("D", 5), | |
rep("M", 3)), | |
levels = c("D", "F", "M") | |
) | |
f_1 | |
levels(f_1) # Get the levels | |
# What happens if we do not specify levels explicitly? | |
f_2 <- factor( | |
x = c(rep("F", 4), | |
rep("D", 5), | |
rep("M", 3)) | |
) | |
f_2 | |
levels(f_2) # Get the levels | |
# What happens if the vector contains elements not pre-specified as levels? | |
f_3 <- factor( | |
x = c(rep("F", 4), | |
rep("D", 5), | |
rep("M", 3)), | |
levels = c("F", "M") | |
) | |
f_3 | |
levels(f_3) # Get the levels | |
typeof(f_3) | |
is.factor(f_3) | |
## data.frames and tibbles----------------------- | |
l_1 <- list( | |
"gender" = c(rep("male", 3), rep("female", 2)), | |
"height" = c(189, 175, 180, 166, 150) | |
) | |
df_1 <- data.frame( | |
"gender" = c(rep("male", 3), rep("female", 2)), | |
"height" = c(189, 175, 180, 166, 150) | |
) | |
tb_1 <- tibble::as_tibble(df_1) | |
# What’s the difference between [ and [[? | |
tb_1["gender"] # Returns the column as a one-column tibble | |
tb_1[["gender"]] # Returns the underlying vector | |
c(tb_1[["gender"]], "male") # works as expected | |
c(tb_1["gender"], "male") # produces weird result | |
# How do you think you can test for the type of a column vector? | |
typeof(tb_1[["gender"]]) # NOT typeof(tb_1["gender"]) | |
dplyr::glimpse(tb_1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment