Basic features - exercise sheet

These exercises are partially derived from Software Carpentry teaching materials available under the CC BY 4.0 license:

https://swcarpentry.github.io/r-novice-gapminder/

https://datacarpentry.org/R-ecology-lesson/

★ marks the most important exercises. Focus on these if the content is completely new to you.
“Optional” marks more challenging exercises. Do these if you have time and find the other exercises easy.


Block 1: creating a project, working with objects

Lecture materials:

Learning goals: learn how to store values as objects


1.1 ★ In RStudio, create a project called RIntro (note that R is case-sensitive).

1.2 ★ Create the following objects. What are the values after each statement?

mass <- 47.5            # mass?
age  <- 122             # age?
mass <- mass * 2.0      # mass?
age  <- age - 20        # age?
mass_index <- mass/age  # mass_index?

1.3 Take a moment to try out different mathematical expressions until you get the hang of it.

You can use the objects you already created as initial values or as intermediates. You can also try adding annotations to your code and removing (as well as re-creating) your objects. Remember the guidelines we covered earlier for naming objects!


Block 2: functions and vectors

Lecture materials: Writing and annotating R code

Learning goals:

  • store multiple values as vectors: use c() to create different types of vectors
  • use functions and modify how a function works with arguments
  • understand R data types

2.1 ★ Round the number 3.49339 to three decimal points.

round(3.49339, digits = 3)

2.2 ★ Create a numeric vector called “weights” using the numbers: 23, 55, 97, 12.

weights <- c(23, 55, 97, 12)

2.3 ★ Create a character vector called “names” using these animal names: cat, tortoise, alpaca.

names <- c("cat", "tortoise", "alpaca")

2.4 We’ve seen that vectors can be of type character, numeric (or double), integer, and logical. What happens if we try to mix these data types in a single vector as in these examples? (hint: check the environment panel or use class() to check the data type of your objects)

num_char <- c(1, 2, 3, "a")
num_logical <- c(1, 2, 3, TRUE)
char_logical <- c("a", "b", "c", TRUE)
tricky <- c(1, 2, 3, "4")

Vectors can be of only one data type. R implicitly converts the data to be all the same type. R tries to convert (coerce) the content of the vector to find a “common denominator” that doesn’t lose any information.

2.5. You’ve probably noticed that objects of different types get converted into a single, shared type within a vector. In R, we call converting objects from one class into another class coercion. These conversions happen according to a hierarchy, whereby some types get preferentially coerced into other types. Can you draw a diagram that represents the hierarchy of how these data types are coerced?

logical → numeric → character ← logical


Block 3: continuing with vectors

Lecture materials: Writing and annotating R code

Learning goals:

  • removing missing values from a vector
  • conditional subsetting to select one or several values from a vector

3.1 ★ Using this vector of heights in inches, create a new vector, heights_no_na, with the NAs removed.

heights <- c(63, 69, 60, 65, NA, 68, 61, 70, 61, 59, 64, 69, 63, 63, NA, 72, 65, 64, 70, 63, 65)

heights_no_na <- heights[!is.na(heights)] 
# or
heights_no_na <- na.omit(heights)
# or
heights_no_na <- heights[complete.cases(heights)]

3.2 ★ Use the function median() to calculate the median of the heights vector.

median(heights, na.rm = TRUE)

3.3 Use R to figure out how many people in the set are taller than 67 inches.

To make things easier to remember, here is the list of comparisons and operators we discussed earlier (this one is useful to memorize):

## different comparisons:
# less than `<`
# greater than `>`
# less or equal `<=`
# greater or equal `>=`
# equal to `==`
# not equal to `!=`

## logical operators:
# and `&`
# or `|`
# not `!` 
heights_above_67 <- heights_no_na[heights_no_na > 67]
length(heights_above_67)

Optional: Block 4: finding help

Lecture materials: Finding help

Learning goals: use ? or help() to access help files of R packages and functions


4.1 Look at the help for the c() function. What kind of vector do you expect you will create if you evaluate the following?

c(1, 2, 3)
c('d', 'e', 'f')
c(1, 2, 'f')
?c

The c() function creates a vector, in which all elements are the same type. In the first case, the elements are numeric, in the second, they are characters, and in the third they are characters: the numeric values are “coerced” to be characters.

4.2 Try to figure out how to use a completely new function based only on its documentation. Look at the help for the function sample() and find out how to use sample() to produce

  • a Finnish lottery ticket: 7 randomly drawn numbers out of 40
  • a vector of 0s and 1s, representing 10 coin tosses

Tip: look at the section Examples in the end.

sample(1:40, 7)
# sample(40, 7)

sample(c(0, 1), 10, replace = TRUE) # sampling with replacement
# sample(0:1, 10, replace = TRUE)
# sample(2, 10, replace = TRUE)