library(learnr) knitr::opts_chunk$set(echo = TRUE)
knitr::include_graphics("images/viktor-nikolaienko-uosfneTS2eQ-unsplash.jpg")
Photo by Viktor Nikolaienko on Unsplash
You made it! You survived the first session and now it's time to try out some R.
We will use self-guided tutorials throughout the workshop.
Just read the instructions and test your skills by writing and running code in the dedicated boxes.
If you see three dots ...
in the code box, you have to delete them and write code instead based on the prompt.
Sometimes there will be multiple-choice questions. Pick the correct answer and hit Submit Answer
to check it.
When you are ready to move to the next section, just click Next topic
or Continue
.
If you get stuck, ask the instructor for help!
Enjoy 🙂
Let's start with some basic mathematical operations.
Run the following code to see the results.
14 + 75 76.5 - 13.46 5 * 2598 4209 / 3
Now is your time to try!
Write the code to calculate the following:
Sum 5 with 8 and multiply by 3 (should be 39
).
Divide 60 by the sum of 5 and 3 (should be 7.5
).
Divide the sum of 76 and 43 multiplied by 5, by the difference between the product of 5 and 3 and the product of 7 and 8 (should be -14.5122
).
# 1. ... # 2. ... # 3. ...
Let's try creating some variables.
Assign the value 4
to a variable called fruit
.
Then subtract 1 from it.
# Create the variable ... # Subtract 1 from it ...
We can assign more than a value to a variable, as long as they are all of the same type (like numeric or a string).
question( "Which of the following functions can be used to assign multiple values to a variable?", answer("`cat()`"), answer("`print()`"), answer("`c()`", correct = TRUE), answer("`t()`") )
Now create a variable houses
with the values "Gryffindor", "Slytherin", "Hufflepuff"
.
houses <- ...
Oops! We forgot one Hogwarts house. Let's fix that!
question( "Which of the following lines of code let us add an extra value to `houses`?", answer('`houses <- c(houses, "Ravenclaw")`', correct = TRUE), answer('`houses <- c("Ravenclaw")`'), answer('`houses + "Ravenclaw"`') )
Now write the code here and run it to add the missing house!
houses ...
question( "Which of the following is true?", answer("A function needs at least one argument."), answer("Functions are strings."), answer("A function can take one value."), answer("Functions can take any number of arguments.", correct = TRUE) )
Run the following code to find out the mean number of fish spotted in the Water of Leith over four days. 🐡🐠🐟
fish <- c(1, 6, 2, 10) sum(fish) / length(fish) mean(fish)
question( "What does the `lenght()` function do?", answer("It calculates the sum of the numbers in the variable."), answer("It returns the size of the fish."), answer("It returns the number of values in a variable.", correct = TRUE) )
Awesome! You finished the first tutorial.
We hope you are looking forward to the next.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.