6 Exercise 1: Test your knowledge
After working through Exercise 1, you’ll…
- have assessed how well you know R and RStudio
- know what chapters and concepts you might want to repeat again
- have managed to apply the basic concepts of R to data
6.1 Task 1
Below you will see multiple choice questions. Please try to identify the correct answers. 1, 2, 3 and 4 correct answers are possible for each question.
1. What panels are part of RStudio?
- source
- console
- input
- packages, files & plots
2. How do you activate R packages after you have installed them?
- import.packages()
- install.packages()
- package()
- library()
3. How do you create a vector in R with elements 1, 2, 3?
- cbind(1,2,3)
- cb(1,2,3)
- c(1,2,3)
- cmb(1,2,3)
4. Imagine you have a vector called ‘vector’ with 10 numeric elements. How do you retrieve the 8th element?
- vector[-2]
- vector[„-2“]
- vector[8]
- vector[„8“]
5. Imagine you have a vector called ‘hair’ with 5 elements: brown, black, red, blond, other. How do you retrieve the color ‘blond’?
- hair[4]
- hair[„4“]
- hair[blond]
- hair[„blond“]
6.2 Task 2
Create a numeric vector with 8 values and assign the name age to the vector. First, display all elements of the vector. Then print only the 5th element. After that, display all elements except the 5th. Finally, display the elements at the positions 6 to 8.
6.3 Task 3
Create a non-numeric, i.e. character, vector with 4 elements and assign the name eye_color to the vector. First, print all elements of this vector to the console. Then have only the value in the 2nd element displayed, then all values except the 2nd element. At the end, display the elements at the positions 2 to 4.
6.4 Task 4
Create a data frame called data. The data frame should contain the following variables (in this order):
- a vector called food. It should contain 5 elements, namely the names of your five favorite dishes.
- a vector called description. For every dish mentioned in food, please describe the dish in a single sentence (for instance, if the first food you describe is “pizza”, you could write: “This is an Italian dish, which I prefer with a lot of cheese.”)
- a vector called rating. Rate every dish mentioned in food with 1-5 (using every number only once), i.e., by rating your absolute favorite dish out of all five with a 1 and your least favorite dish out of all five with a 5.
Hint: For me, the data frame would look something like this:
## food description Rating
## 1 pizza Italian dish, I actually prefer mine with little cheese 3
## 2 pasta Another Italian dish 1
## 3 ice cream The perfect snack in summer 2
## 4 crisps Potatoes and oil - a luxurious combination 4
## 5 passion fruit A fruit that makes me think about vacation 5
6.5 Task 5
Can you sort the data in your prior data set by rating - with your favorite dish (i.e., the one rated “1”) on top of the list and your least favorite dish (i.e., the one rated “5”) on the bottom?
Important: You do not yet know this command - you’ll have to google for the right solution. Please do and note down the exact search terms you used for googling.
Hint: For me, the data frame would look something like this:
## food description Rating
## 1 pasta Another Italian dish 1
## 2 ice cream The perfect snack in summer 2
## 3 pizza Italian dish, I actually prefer mine with little cheese 3
## 4 crisps Potatoes and oil - a luxurious combination 4
## 5 passion fruit A fruit that makes me think about vacation 5
When you’re ready to look at the solutions, you can find them here: Solutions for Exercise 1.
Now that you’re well versed in data types and structures, let’s move on to data management in the tidyverse in the Tutorial: Data management with tidyverse.