Created
April 6, 2022 11:53
-
-
Save graebnerc/bf8b1b60084377744881b392f0c9534e to your computer and use it in GitHub Desktop.
Session Script T5, Advanced object types (April 6, 2022)
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
# Session Script T5, Advanced object types (April 6, 2022) | |
# Factors - Slide 10 | |
f_1 <- factor(c(rep("F", 2), rep("M", 3), rep("D", 3)), | |
levels = c("D", "F", "M")) | |
f_1 | |
f_2 <- factor(c(rep("F", 2), rep("M", 3), rep("D", 3))) | |
f_2 | |
f_3 <- factor(c(rep("F", 2), rep("D", 3))) | |
f_3 | |
f_4 <- factor(c(rep("F", 2), rep("M", 3)), | |
levels = c("D", "F", "M")) | |
f_4 | |
f_5 <- factor(c(rep("F", 2), rep("M", 3), rep("D", 3)), | |
levels = c("D", "M")) | |
f_5 | |
table(f_4) | |
table(f_5) | |
# Data frames and tibbles - slide 13 | |
df_1 <- data.frame( | |
"c1"= seq(1, 2, length.out=4), | |
"c2" = seq(4, 5, length.out=4), | |
"c3" = c(rep(TRUE, 2), rep(FALSE, 2)) | |
) | |
tb_1 <- tibble::as_tibble(df_1) | |
# Quick exercises - slide 15 | |
# Create a factor with the levels “still”, “medium” and “sparkling”, | |
# and arbitrary instances of the three levels | |
f1 <- factor(c(rep("still", 4), rep("medium", 5), rep("sparkling", 3))) | |
# Get the relative frequencies for “medium” of this factor | |
# Häufigkeiten von allen Elementen | |
abs_freqs <- table(f1) | |
# Häufigkeiten teilen durch Anzahl an Elementen | |
n_elements <- length(f1) | |
# Compute relative freqs: | |
round(abs_freqs / n_elements * 100, 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 | |
# Create columns | |
nb_ <- seq(1, 5) | |
char_ <- as.character(seq(6, 10)) | |
# Create data frame | |
df_s_a <- data.frame( | |
"nb"=nb_, | |
"char"=char_ | |
) | |
df_s <- data.frame( | |
"nb"=as.double(seq(1, 5)), | |
"char"=as.character(seq(6, 10)) | |
) | |
# Transform this data frame into a tibble! | |
tb_s <- tibble::as_tibble(df_s) | |
tb_s | |
# Extract the second column of this tibble such that you have a vector | |
tb_s[["char"]] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment