R Bootcamp Exercises: Loops, Flow Statments, and Functions

  1. Data imputation is frequently a necessity in data analysis. One common method, though not necessarily the best one, is to impute missing data with the mean of available data.
  1. First, create a vector, x, with all integers between 1 and 100.
  1. Write a function that calculates the mean of every column in a dataframe. Code the function so that it evaluates the column mean if the column elements are numeric, using class(x) == “numeric”. Try your function on the iris dataset.

  2. Write a function that checks every element in a matrix, and if an element is NA it changes it with a 0. Use a for loop in your function. Make sure your function is generalizable to matrices of any size, as long as nrow>1 and ncol>1. Check whether your function works as intended on the penguin data set and on a matrix you generated.

  3. Create a list with following elements: