Created
April 18, 2024 09:30
-
-
Save graebnerc/de4c233f8901905a5d4c15a586547801 to your computer and use it in GitHub Desktop.
Solutions to the intermediate exercises of the session on basic object types.
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
# Task 1: Intermediate exercises I=============== | |
# Create a vector containing the numbers 2, 5, 2.4 and 11. | |
vec_task_1 <- c(2, 5, 2.4, 11) | |
# What is the type of this vector? | |
typeof(vec_task_1) | |
# Transform this vector into the type integer. What happens? | |
typeof(vec_task_1) | |
as.integer(vec_task_1) # Does not modify the original vector | |
typeof(vec_task_1) | |
vec_task_1_int <- as.integer(vec_task_1) # Modifies the original vector | |
typeof(vec_task_1_int) | |
# Do you think you can create a vector containing the following elements: | |
# "2", "Hello", 4.0, and TRUE? | |
mixed_vec <- c("2", "Hello", 4.0, TRUE) | |
mixed_vec | |
# See the tutorial for an explanation of the underlying hierarchy and | |
# why the type is as follows: | |
typeof(mixed_vec) | |
# Task 2: Intermediate exercises II============== | |
# Create a vector with the numbers from -2 to 19 (step size: 0.75) | |
seq_vec <- seq(-2, 19, by=0.75) | |
seq_vec | |
# Create an index vector for this first vector (note: an index vector is a | |
# vector with all possible indices of the original vector) | |
seq_vec_index <- seq_along(seq_vec) | |
seq_vec_index | |
# Compute the log of each element of the first vector using vectorisation. | |
# Anything that draws your attention? | |
log(seq_vec) | |
# Logs for negative values do not exist, so NaNs were added | |
# What happens if you concatenate vectors of different types using c()? | |
# Can you derive a systematization? | |
v_double <- c(2.0, 3.8) | |
v_int <- c(1L, 3L) | |
v_char <- c("1", "99") | |
typeof(c(v_double, v_int)) | |
typeof(c(v_char, v_int)) | |
# The concatenated vector is coerced into the type of the original vector | |
# according to the standard type hierarchy | |
# Task 3: Intermediate exercises III============= | |
# Create a list that has three named elements: "A", "B", and "C" | |
# The element "A" should contain the square root of the | |
# numbers form -2 to 8 (step size: 1) | |
# The element "B" should contain the log of numbers between 2 and 4 | |
# (step size: 0.5) | |
# The element "C" should contain letters from a1 to g7 | |
# (hint: use the pre-defined vector letters and the function paste()) | |
li_1 <- list( | |
"A"=sqrt(seq(-2, 8, by=1)), | |
"B"=log(seq(2, 4, by=0.5)), | |
"C"=paste(letters[1:7], 1:7, sep = "") | |
) | |
li_1 | |
# Recommendable: | |
a_element <- sqrt(seq(-2, 8, by=1)) | |
b_element <- log(seq(2, 4, by=0.5)) | |
c_element <- paste(letters[1:7], 1:7, sep = "") | |
li_1 <- list( | |
"A"=a_element, | |
"B"=b_element, | |
"C"=c_element | |
) | |
li_1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment