3 Homeworks

Session 1: Before you learn the language

Create an rProject using Rstudio incl. a folder for your R code and your data.

Session 2: Introduction to R

  1. Read the intro of the Tidyverse coding style guide. Read on during the course whenever in doubt what the correct style is.
  2. Download the script and try to understand the content.
  3. Then, create 3 vectors: A logical, a character and a numeric one that have an equal length of 5.
  4. Create a list that contains all the vectors.
  5. Take one of the vectors and select:
    • the last element
    • all except the first element
  6. Then take the list and select:
    • the first element as a list
    • the second element of the third element in the list.

Session 3: Working with dataframes

  1. Go through the script, read the comments and understand the code.
  2. Create a new R file in your project and import your own data in R as a dataframe.
  3. How many rows and columns does your dataframe have?
  4. What are the datatypes of the vectors within your dataframe?
  5. Create another new R file called dataframes.R. Open it. In R, there is a preloaded dataframe called “mtcars”. Using only the methods we learned today, try to perform the following actions:
    • Take a look at the first 10 rows and last 10 rows
    • Create a new dataframe that only retains the columns mpg, disp and hp of cars that have 6 cylinders (cyl is the column for the number of cylinders). First use the base R method using the brackets. Then use dplyr functions to achieve the same!
    • if you did not already: answer the same question but use the %>% operator to get to your answer!
    • How many observations are in this new dataframe?
    • export that dataframe to an excel file in your data folder

Session 4: Data manipulation, functions and conditional execution
1. Download the script and try to understand the content.
2. Structure and manipulate your own data as far as you can. Also obtain useful summary statistics of your own data by making use of group_by and summarise functions.
3. Write a function that takes as arguments two numeric vectors and returns a named list that shows the Pearson as well as the Spearman correlation of these vectors.
4. Now rewrite this function by giving it another argument (linear = FALSE). Write the function so that it returns Pearson correlation when linear == TRUE and otherwise Spearman correlation.

Session 5: ggplot2 basics and iteration
1. Download the script and try to understand the content.
2. Use your own data to create a scatterplot with a regression line
3. A study was done on 100 participants. For each participant the same data was collected and stored in a csv file. The data is in homework5.zip. Use a for loop (and/or map()) to import each datafile to a dataframe in R. You want to combine all these to a single dataframe in the end. Do not use any other tools than those that we learned so far. There is one exception and that is the function bind_rows() that you may use. If you have no idea how to accomplish this, check this hints file file. But try it without.

Session 6: ggplot2
1. Download the script and try to understand the content.

Session 7: Basics of Linear Regression
1. Download the script and try to understand the content.
2. Go through this site and see how to do the most common analyses in R.

Session 8: Using Rmarkdown for scientific reporting
- complete the in class assignment if you did not finish it.