C3 - Exercise Sheet

Here you find the exercise sheet for chapter 3: “Data Manipulation”

Start a project and import data

Task 1

Create an R project for solving this Exercise Sheet.

Task 2

Download the csv-file SSRC_data.csv and the R script SSRC_C3_template.R and put it in the R project folder you created in Task 1.

Task 3

Open the SSRC_C3_template.R R Script.

Task 4

Use the read.csv() command to load the SSRC data into R and call the respective data object SSRC_data.

Task 5

Get a first impression of the dataset by checking out the first 6 rows of the dataset.

Task 6

Install and load the tidyverse package. (If you have already installed the package before, loading the package is sufficient)

The mutate() command

Task 7

Create a new variable that contains the age of an individual in months. Call the variable age_in_months and add it to the SSRC dataset. Check out the first six rows of the dataset after creating this new variable.

Check variable types

Task 8

Use the str() command to check the variable types in the SSRC data.

Factor and Indicator Variables

Task 9

Transform the variables gender, education_level and physical_activity_level into factor variables. Use the str() command to check out whether it worked.

Task 10

Check the level order of the variable physical_activity_level and adjust it if necessary to “low”, “medium,”high”.

Task 11

Use the mutate() command to create a logical variable indicating whether an individual has a bmi larger than 25 and call this variable overweight_indicator. Check the class of the new variable using the class() and str() command.

Task 12

Change the name of the variable gender into sex. Use the str() command to check whether it worked.

Task 13

Create a new factor variable that indicates whether an individual is “underweight” (bmi < 18.5), “normal” (bmi between 18.5 and 25) or “overweight” (bmi > 25). Call this variable weight_category. Use the str() and head() command to check out whether it worked out.