Solutions R week 1

Author

Janpieter van der Pol

Exercises part 3

Question 1 – Create a Matrix

# Create a 4x4 matrix filled with zeros
zero_matrix <- matrix(0, nrow = 4, ncol = 4)

zero_matrix

# Variable’s name: zero_matrix
# Type of object: a "matrix" storing numeric values
class(zero_matrix)   # shows "matrix"

Question 2 – Variable Assignment

# Assign 10 to a variable
my_number <- 10
my_number

# Assign a new value (20) to the same variable
my_number <- 20
my_number

# The old value (10) is replaced by the new value (20). 
# A variable can be updated; it always keeps the last value assigned.

Question 3 – Matrix Operations

We reuse zero_matrix from Question 1

# 3a. Add 5 to each element in the matrix
matrix_plus_5 <- zero_matrix + 5
matrix_plus_5

# 3b. Multiply the matrix by 9
matrix_times_9 <- zero_matrix * 9
matrix_times_9

# 3c. Matrix multiplication of the matrix by itself
# (Using the %*% operator)
matrix_mult <- zero_matrix %*% zero_matrix
matrix_mult

Question 4 – Practice chaining functions

Create a vector from 1 to 20 Then turn it into a matrix Then get the maximum Then compute the log of this maximum

library(tidyverse)
result_log_max <- 1:20 |>
  as.matrix() |> 
  max() |> 
  log()

result_log_max
library(tidyverse)
# Alternative solution using the tidyverse pipe:
result_log_max <- 1:20%>% 
  as.matrix() %>% 
  max() %>% 
  log()

result_log_max

Exercises part 4

1. Check numeric values

# Assign numeric values to two variables
num_a <- 10
num_b <- 3.14

# Check the format (type) of the variables
class(num_a)       # "numeric"
class(num_b)       # "numeric"

is.numeric(num_a)  # TRUE
is.numeric(num_b)  # TRUE

2. Check textual values

# Assign textual (character) values to two variables
text_a <- "Hello"
text_b <- "Biotech"

# Check the format (type) of the variables
class(text_a)        # "character"
class(text_b)        # "character"

is.character(text_a) # TRUE
is.character(text_b) # TRUE

3. Transform values: number -> text in R

# Start with a numeric variable
my_number <- 42
class(my_number)       # "numeric"

# Transform the number into text (character) in R
my_number_text <- as.character(my_number)
my_number_text
class(my_number_text)  # "character"

# Check that the transformation worked
is.character(my_number_text)  # TRUE

Exercises part 5

1. Equality Check (x and y)

# Create two variables with different values
x <- 5
y <- 10

# Check if x is NOT equal to y
x_not_equal_y <- x != y
x_not_equal_y   # Should be TRUE because 5 is not equal to 10

# Now try making x and y equal
x <- 5
y <- 5

x_not_equal_y <- x != y
x_not_equal_y   # Now this should be FALSE

2. Inequality Check (a and b)

# Create two variables
a <- 3
b <- 7

# Check if a is equal to b
a_equal_b <- a == b
a_equal_b   # FALSE, because 3 is not equal to 7

# Change the values so they are equal
a <- 4
b <- 4

a_equal_b <- a == b
a_equal_b   # TRUE, because 4 is equal to 4

3. Multiple Conditions (temperature and humidity)

# Set temperature and humidity
temperature <- 25   # greater than 20
humidity    <- 40   # less than 50

# Check BOTH conditions:
# temperature > 20 AND humidity < 50
good_conditions <- (temperature > 20) & (humidity < 50)

good_conditions   # TRUE if both conditions are satisfied

Exercises part 6

1. Concatenating Text and Numbers

# Generate a random number between 4 and 87
random_number <- runif(1, min = 4, max = 87)
random_number

# Print: "The generated number is x"
sentence_random <- paste("The generated number is", random_number)
sentence_random

# Generate an integer between 1 and 70 (different function)
integer_number <- sample(1:70, 1)
integer_number

integer_sentence <- paste("The generated integer is", integer_number)
integer_sentence

2. Arithmetic Results in Sentences

# Create two variables a and b
a <- 8
b <- 3

# Calculate sum, difference, and product
sum_ab  <- a + b
diff_ab <- a - b
prod_ab <- a * b

# Print the results in sentences
paste("The sum of a and b is", sum_ab)
paste("The difference (a - b) is", diff_ab)
paste("The product of a and b is", prod_ab)

3. Boolean Comparisons in Text

# Create two numbers x and y
x <- 10
y <- 15

# Check if x is greater than y
comparison_result <- x > y
comparison_result   # This will be TRUE or FALSE

# Print a sentence including the result
paste("It is", comparison_result, "that x is greater than y.")