Skip to content

Instantly share code, notes, and snippets.

@graebnerc
Last active March 29, 2023 08:00
Show Gist options
  • Save graebnerc/2682bb2d872eb613bdea70f6f9f974b9 to your computer and use it in GitHub Desktop.
Save graebnerc/2682bb2d872eb613bdea70f6f9f974b9 to your computer and use it in GitHub Desktop.
Lecture notes and solutions to the exercises of session 2 in the spring semester 2023
Lecture notes and solutions to the exercises of session 2 in the spring semester 2023
# Session 2 - Exercise solutions I
# Execute the following mathematical computations via the console:
# a)
5 + 12
# b)
(2*3)**2
# c)
2*5.8
# d)
(8**2 + 5**4)/3
((8**2) + (5**4))/3
# Session 2 - Exercise solutions II
a_ <- 2+3
b_ <- (5*a_)/2
c_ <- (b_ + 1)**2
d_ <- sqrt(c_)
d_
# Session 2 - Exercise solutions III
# Define a vector with the elements -2, 2, 4, 6, 9 and NA
t_vec <- c(-2, 2, 4, 6, 9, NA)
# Apply the following functions and understand what they are doing:
median(t_vec)
median(t_vec, na.rm = TRUE)
# Computes the median
is.na(t_vec)
# Gives TRUE/FALSE for each element of t_vec, indicating of the
# element is missing or not
anyNA(t_vec)
# Indicates whether there is any missing element
sum(t_vec, na.rm = TRUE)
# Sums up all elements
var(t_vec, na.rm = TRUE)
# Computes the sample variance (check out help(var), if in doubt)
# More info:
# 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
# Session 2 - Exercise solutions IV
#' 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
# Lecture notes session 2
# 1. R as a calculator-----------------
2 + 3 # Addition
6 - 8 # Substraction
5/3 # Division
4*2 # Multiplication
5**2 # Exponentiation
(3-2)*2 # Using brackets
# 2. Assignments ----------------------
assign("intermediate_result", 4-2)
intermediate_result
final_result <- intermediate_result*5
TRUE <- 5**2 # Gives an error
rm(final_result) # Removes name-object association
# 'One object - many names' is possible:
name_1 <- 66**2
name_2 <- 66**2
name_2 <- name_1
# One name can only be used for at most one object:
name_1 <- 66**2*4
name_1 <- 66**2*4*5 # Overwrites previous assignment
# Digression: you can concatenate objects and refer to them with one name:
name_1_2 <- c(name_1, name_2)
# Be aware between the distinction of names and objects:
# Gives an error:
assign(a, 5)
# Assigns the name 'name_of' to 5:
a <- "name_of"
assign(a, 5)
# Assigns the name 'a' to 5:
assign("a", 5)
# 3. Defining functions ----------------------
# Solution for the short practice on slide 27:
function1 <- function(x1, x2){
eq_1 <- x1**2 + 2*x1*x2 + x2**2
return(eq_1)
}
# Example: writing a function to normalize the vector
# For a more detailed solution see file S2-ExerciseSolutions-IV.R
x_vec <- c(2, 4, 8)
sigma <- function(vector_1){
min_vec_1 <- min(vector_1)
max_vec_2 <- max(vector_1)
step1 <- vector_1 - min_vec_1
step2 <- max_vec_2 - min_vec_1
step3 <- step1/step2
return(step3)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment