Last active
September 24, 2022 06:20
-
-
Save graebnerc/8569172d758ea2a4c5f9305dc3ce9966 to your computer and use it in GitHub Desktop.
Lecture notes and solutions to the exercises of session 2 in the fall semester 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
Lecture notes and solutions to the exercises of session 2 in the fall semester 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
# Solutions to the in-class exercises of session 2 | |
# Exercise 2.1 (slide 9)--------------- | |
# Convention: white space between + or -, | |
# but no white space between *, **, and / | |
5 + 12 | |
(2*3)**2 | |
2*5.8 | |
(8**2 + 5**4)/3 | |
# Exercise 2.2 (slide 16)-------------- | |
# I used these names to avoid using 'c' as a name | |
# since c() is also a function | |
a_ <- 2 +3 | |
b_ <- (5*a_) / 2 | |
c_ <- (b_+1)**2 | |
d_ <- c_**0.5 | |
d_ | |
# Function exercises I (slide 24)------ | |
f_vec <- c(-2, 2, 3, 6, 9, NA) | |
median(f_vec) | |
median(f_vec, rm.na=TRUE) | |
is.na(f_vec) # Tests each element for NA | |
anyNA(f_vec) # Tests whether vector contains at least one NA | |
sum(f_vec) | |
sum(f_vec, rm.na=TRUE) | |
# From the help function you may know that | |
# var() computes sample variances | |
var_vec <- c(1, 6, 7, 9, 12) | |
sample_var <- var(var_vec) | |
# To get the population variance: | |
# Determine length of data | |
n_elements <- length(sample_var) | |
pop_var <- var(sample_var)*(n_elements - 1)/n_elements | |
# See, e.g., here: https://stackoverflow.com/a/37733398 | |
sample_var | |
pop_var | |
# Function exercises II (slide 28)----- | |
#' Normalize a vector into 0-1 range | |
#' | |
#' This function takes a vector and normalizes its elements into the range | |
#' between 0 and 1. It does not yet handle special cases, such as vectors with | |
#' missing values, but we learn how to deal with this later on. | |
#' @param x_vector A vector without missing values | |
#' @returns A vector with the same number of elements as `x_vector`, but | |
#' normalized into the 0-1 range | |
norm_function <- function(x_vector){ | |
min_value <- min(x_vector) | |
max_value <- max(x_vector) | |
result <- (x_vector - min_value) / (max_value - min_value) | |
return(result) | |
} | |
# Example application: | |
vec_used <- c(1, 2, 3, 4, 5, 6, -2) | |
vec_used_normed <- norm_function(vec_used) | |
vec_used_normed | |
# Note that all values defined within the function are not available after | |
# calling it: | |
max_value | |
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 2 notes | |
# These are all notes that I have taken during the lecture | |
# Solutions for the exercises can be found in a separate script | |
# Assignments-------------------------- | |
assign("intermediate_result", 2 + 3) | |
assign("complicated_result", (8**2 + 5**4) / 3) | |
assign("other_result", (2*3)**2) | |
other_result <- (2*3)**2 # Equivalent to line 10 | |
other_result = (2*3)**2 # Do never do that!!! | |
rm(other_result) | |
# Many names, one object?-------------- | |
a_name <- 2*2 | |
b_name <- 2*2 | |
a_name | |
b_name | |
# Many objects, one name?-------------- | |
c_name <- 2*3 | |
c_name <- 2*4 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment