Exercises

In this practical we are going to play around with the different types of elements in R.


  1. Make two vectors: one named vec1 with values 1 through 6 and one named vec2 with letters A through F.

  1. Create two matrices, one from vec1 and one from vec2. The dimensions for both matrices are 3 rows by 2 columns.

  1. Inspect your vectors and matrices. Are all numerical?

  1. Make a matrix from both vec1 and vec2 with 6 rows and 2 columns. Inspect this matrix.

  1. Make a dataframe called dat3 where vec1 and vec2 are both columns. Name the columns V1 and V2, respectively. Use function data.frame().

  1. Again, make a dataframe called dat3b where vec1 and vec2 are both columns. Name the columns V1 and V2, respectively. Use function as.data.frame() on the matrix obtained from Question 4.

  1. Check if the first column in the data frames from Question 4 and Question 5 are indeed numeric. If not, determine what they are.

  1. Select 1) the third row, 2) the second column and 3) the intersection of these two in the dataframe dat3 that you have created in Question 4.

  1. Imagine that the first variable V1 in our dataframe dat3 is not coded correctly, but actually represents grouping information about cities. Convert the variable to a factor and add the labels Utrecht, New York, London, Singapore, Rome and Cape Town.

  1. Open the workspace boys.RData. You need to download this file and put it in the project folder.

  1. Most packages have datasets included. Since we have not learned to load packages yet, you are presented with such a data set in a workspace. Open the boys dataset (it is from package mice, by the way) by typing boys in the console, and subsequently by using the function View().

  1. Find out the dimensions of the boys data set and inspect the first and final 6 cases in the data set.

  1. It seems that the boys data are sorted based on age. Verify this.

  1. Inspect the boys dataset with str(). Use one or more functions to find distributional summary information (at least information about the minimum, the maximum, the mean and the median) for all of the variables. Give the standard deviation for age and bmi. Tip: make use of the help (?) and help search (??) functionality in R.

  1. Select all boys that are 20 years or older. How many are there?

  1. Select all boys that are older than 19, but younger than 19.5. How many are there?

  1. What is the mean age of boys younger than 15 years of age that do not live in region north?

End of practical