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 in the my-work directory, if not
already created (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 using the function round().
2.2 ★ Create a numeric vector called “weights” using the numbers: 23, 55, 97, 12.
2.3 ★ Create a character vector called “names” using these animal names: 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")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?
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.
3.2 ★ Use the function median()
to calculate the median of the heights vector.
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 `!` 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?
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.