Introduction to R - exercises

Please follow the following exercises to get familiar with R. Parts of the exercises follow A (very) short introduction to R by Paul Torfs & Claudia Brauer, Hydrology and Quantitative Water Management Group Wageningen University, The Netherlands

Task 1

Compute the difference between 2012 and the year you started at your university and divide this by the difference between 2012 and the year you were born. Multiply this with 100 to get the percentage of your life you have spent at this university. Use brackets if you need them.

Task 2

Find help for the log function.

Task 3

Repeat Task 1, but with several steps in between. You can give the variables any name you want, but the name has to start with a letter. Additionally, calculate the following:


\[ \frac{5}{5+345} \]


\[ 2 \cdot \sin (90°) \] Mind the conversion between degrees and radians!


\[ \sqrt{16}+\sqrt{25} \]


\[ \frac{\frac{5}{5+345}+2 \cdot \sin (90°)}{\sqrt{16}+\sqrt{25}} \]

Task 4

Compute the sum of 4, 5, 8 and 11 by first combining them into a vector and then using the function sum. What are mean and median of this series?

Task 5

Plot 100 random numbers following a Gaussian distribution with mean 11 and standard deviation 42 as a series of connected points and add their the mean as a horizontal grey line. Additionally, plot these random numbers in a histogram (showing densities on the y-axis) and as box plot.

Task 6

Put the numbers 31 to 60 in a vector named p and in a matrix with 6 rows and 5 columns named q. Calculate the row- and column-wise sum of q.

Task 7

Construct three random standard normal vectors of length 100. Call these vectors x1, x2 and x3. Make a data frame called t with three columns (called Va, Vb and Vc) containing respectively x1, x1+x2 and x1+x2+x3. Call the following functions for this data frame: plot(t) and cov(t). For each column call the function sd. Can you understand the results?

Task 8

Compute the mean of the square root of a vector of 100 random numbers. What happens?

Task 9

Make a vector from 1 to 100. Make a for-loop which runs through the whole vector. Multiply the elements which are smaller than 5 and larger than 90 with 10 and the other elements with 0.1.

Task 10

Write a function for the previous Task, so that you can feed it any vector you like (as argument). Use the standard R function length in the specification of the counter.

Task 11

Find a suitable marginal distribution for the Nile dataset:

Task 12

Install all required packages for the course and take a look at their demos and data.




Load your data

Locate your favourite data set you might want to use throughout the course and load it into R. Make sure to save it as a clean .RData file. Mind your current working directory. Set up a new RStudio project for the upcoming week.

Optional extensions:

Install optional packages:

install.packages("spcopula", repos="")

check the demo doing a subset of a multivariate return period analysis in the spcopula package: