Exercise 1

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

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”]

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.

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.

Task 4

Get the “data_tutorial2.csv” from Moodle ( 4. Mai material folder ) and put it into the folder that you want to use as working directory.

Set your working directory and load the data into R by saving it into a source object called data. Note: This time, it’s a csv that is actually separated by commas, not by semicolons.

Task 5

Now, print only the age column to the console. Use the $ operator first. Then try to achieve the same result using the subsetting operators, i.e. [].

Task 6

Print only the first 6 age numbers to the console. Use the $ operator first. Then try to achieve the same result using the subsetting operators, i.e. [].

When you’re ready to look at the solutions, you can find them here: Solutions for Exercise 1.