Exercise 2: Solutions

Task 1.1

Define a vector x containing 100 values between 0 and 10.

x <- seq(0, 10, lengh.out = 100)
Warning: In seq.default(0, 10, lengh.out = 100) :
 extra argument 'lengh.out' will be disregarded

Task 1.2

Define a second variable y using sin(x).

y <- sin(x)

Task 1.3

plot(x, y, pch = 20)

Task 1.4

Do the same, but swap the axis now. However, the axis name should stay the same as before.

plot(y, x, pch = 20, xlab = "x", ylab = "y")

Task 1.5

Now, do the same as a line plot. Add a title. Add the additional argument lty = 2.

plot(x, y, type = "l", lty = 2, main = "Sinus function")

Task 1.6

Combines a scatter and a line plot: make a scatter plot and use the lines() function to add a line.

plot(x, y, type = "l", lty = 2, main = "Sinus function")

plot(x, y, pch = 20)

Task 1.7

Create a new variable x <- c(1:10, 1:20, 1:30, 1:40, 1:50). Plot a histogram using 5 breaks.

x = c(1:10, 1:20, 1:30, 1:40, 1:50)
hist(x, breaks = 5)

Task 1.8

Add a line to the histogram with x-axis values 1:50 and y-axis values 50:1. Use the additional argument lwd = 2.

hist(x, breaks = 5)
lines(1:50, 50:1, lwd = 2)

Task 1.9

Define a data set using the command df <- data.frame(x = c(rnorm(100), rexp(100)), group = rep(1:2, each = 100)).

df <- data.frame(x = c(rnorm(100), rexp(100)), group = rep(1:2, each = 100))

Task 1.10

Make a Boxplot of the variable x.

boxplot(x)

Task 1.11

Make a Boxplot for both variables.

boxplot(x~group, data = df)