Task 1

Take the following vector:

x <- c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)

Write a function that takes x as an argument and computes the sum of the elements in the vector using a for loop. Test it against the sum() function.

Task 2

We now turn towards a relatively easy problem that is often asked as a warm-up in coding interviews.

Write a function that takes an integer number as an argument and puts out the word “fizz” or “buzz” up to that integer. If an integer is divisible by 3, the function should put out “fizz”. If it is divisible by 5, the function should put out “buzz”. If it is divisible by both 3 and 5, the function should put out “fizzbuzz”. If none of the above holds true, the function should simply return the integer as a string.

In a first step, do this using a for loop and if statements. If you do not provide an integer number, the function should throw a meaningfull error message.

Tip: remember the modulo operator!

Task 3

Use map() from the purrr package to apply the class() and typeof() function to each column of the palmerpenguins package.

ADVANCED TASK (skip if you are stuck):

Use map_df to compute a data frame consisting of variables showing the class and type of each column of the penguins data and a variable storing the column names of the penguins data.

Tip: You need to use data.frame() inside map_df() and create the variable you want inside this data.frame() call. Further, look into the .id argument of map to get the column names of the penguin data set.

Task 4

Take the function you defined in Task 2 and rewrite it such that it takes a vector of integers instead and works with purrr::map_chr().