library(learnr) knitr::opts_chunk$set(echo = FALSE) tutorial_options(exercise.timelimit = 60, exercise.blanks = "___+")
To start, familiarize yourself with the interactive R console widget. This allows you to run chunks of R code in your web browser, which is great for interactive tutorials.
In some cases these exercises will be pre-populated with some R code you can modify or just run. For example, try running this chunk of code. Note, you can reset one of these code chunks with the 'start over' button (so don't worry about playing around you can always reset to the initial state)
You can also run a selected code chunk with the Cmd+Return
hotkey
some_text <- 'this is a test' print(some_text)
Now try writing something yourself and see what happens
When you're ready to move on click the 'Next Topic' button. Note that you can also use the Table of Contents menu on the left to jump to a topic.
First play around with using R as a fancy calculator.
Recall the key math operations
*
: multiplication/
: division+
: addition-
: subtraction^
: 'raise to the power'Try testing some mathematical relationships with logical operators
==
equals!=
not equals>
greater than, <
less than>=
greater than or equal to, etc.x|y
x OR y is truex&y
x AND y are true2 == 1+1
`## Creating variables
Make a variable called x
by assigning it a value with <-
. The print(x)
line will print the value stored in x
to the console (so will just entering the variable name). Note: you have to assign x
a value before the print(x)
line for this to work. Check what happens when you print(x)
before assigning it a value.
If you're stuck try the 'hint' button.
print(x)
## Remember that <- is the assignment operator
x <- 5 print(x)
Make two numeric variables named (x
and y
) , add them together, and assign the results to a variable called result
.
Test what happens if you change the value of one of the variables after creating result
. Does it automatically update the value of result?
print(result)
x <- 1 print(result)
x <- 1 y <- 4 result <- x+y print(result)
Test whether R variable names are case-sensitive.
Myvar1 <- 1 print(myvar1) ## It is case sensitive! These are not the same variable
Recall the core types of variables in R:
Numeric: Numbers
Strings (text): Any set of characters bounded by single OR double quotes is treated as a text value
Logical (TRUE/FALSE)
Factors (ordered categorical variables like 'bad', 'good', 'great'). Let's ignore these for now
Try making some strings, and assigning them to a variable my_string
.
my_string <- ___ print(my_string)
What happens if you apply a mathematical operation to strings (i.e. adding two together)?
a <- 1+1 b <- '1'+'1' print(a) print(b)
Note that logical TRUE and FALSE are treated specially in R. These values are given by all-caps without any quotes so they are not strings. See what happens if you try different permutations of TRUE, like TRUE
, True, true, T.
The last one is a bit of a trick! The values T and F actually are treated by R as the same as TRUE and FALSE (can be a useful short-hand but avoid doing this at first as it can cause confusion)
x <- TRUE isTRUE(x)
Observe what happens here as well. Note that the numeric values 1 and 0 are also treated as equivalent to TRUE and FALSE respectively.
0 == FALSE 1 == TRUE 2 == TRUE
Create two logical variables x
and y
and then try combining them with the logical AND and OR operators &
and |
Recall that vectors are essentially just ordered collections of values. Like a sequence of 'buckets'
They can hold data of these various types we've just explored:
Remember that you can use the c()
function to combine values into a vector
Note that all data in a vector has to be the same type!
Try creating a vector called vec
that holds the values 'a'
and 'b'
print(vec)
vec <- c('a', 'b') vec
What happens if you make a vector with a mix of numeric and string values?
c(1, 'c')
You can also use the c()
function to add more values to an existing vector. Try adding another number to vec
vec <- c(1, 2) vec <- ___ print(vec)
vec <- c(1, 2) vec <- c(vec, 3) print(vec)
You can also combine together vectors using c()
(it's quite a flexible function!). Try combining the two vectors
vec1 <- c(1, 2) vec2 <- c(3, 4) vec3 <- ___ print(vec3)
vec1 <- c(1, 2) vec2 <- c(3, 4) vec3 <- c(vec1, vec2) print(vec3)
What happens if you concatenate a string to a vector of numeric values?
vec <- c(1, 2) print(vec)
vec <- c(1, 2) vec <- c(vec, 'cat') print(vec) ## All the elements turn to string
There are a number of specialized functions for making numeric vectors efficiently. For example, you can make a numeric sequence from x
to y
using x:y
.
Try creating a vector containing all integers between 100 and 150, followed by those between -40 and -10.
c(1:10)
c(100:150,-40:-10)
What happens if you use the a:b
approach to make a vector, but the first number is larger than the second?
c(40:10) ## You get a decreasing series of numbers
What happens if you apply a math operation (e.g. add/subtract a number) to a vector of numbers?
vec <- 1:10
vec <- 1:10 vec+2 vec*2 ## The operation is carried out element wise
Create a logical vector indicating which integers between 1 and 10 are divisible by 3 (Hint, %%
is the 'modulus' function. 4 %% 2
is 0 because 4 is evenly divided by 2)
## Check if 6 is divisible by 3 6%%3==0
c(1:10)%%3==0
Recall that lists are basically like relaxed vectors, where the elements don't have to be the same type. You can create them with the list()
function, and combine them with the c()
function.
Make a list that includes a string, a number, and a vector as entries
my_list <- ___ print(my_list)
list('a',1, c(1,2,3))
Add some more elements to the beginning or end of the following list
my_list <- list('a',1, c(1,2,3)) my_list <- ___ print(my_list)
my_list <- list('a',1, c(1,2,3)) my_list <- list('cat', 'dog', my_list) print(my_list)
Now let's make a list of lists! Create a list a
with two lists inside it.
a <-
a
a[1]
a[2]
a <- list(list(1,2), list('a','b'))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.