```{=html}
```r library(learnr) library(submitr) library(googlesheets4) library(gradethis) library(CKteachR) knitr::opts_chunk$set(echo = FALSE) learnr::tutorial_options( exercise.timelimit = 60, exercise.checker = gradethis::grade_learnr) heights_cm <- c(184, 162, 145, 200, 178, 154, 172, 142) heights_m <- heights_cm / 100 # convert cm to m
submitr::login_controls()
options(tutorial.storage = "none") out <- setup_progress_monitoring( rstudioapi::getActiveProject(), "PHDL1_week3", "15oXGGq0fgFL7kN3ZCcouhJhnIY_wJCDj3tPu6nyug-g", "122eUZo4Q3VJ3hTy9ySoOTITPfNkrb1-p4nNeQm78w5I", # week 3 spreadsheet "UoL.MPH.datalab@gmail.com" ) submitr::shiny_logic(input, output, session, out$vfun, out$storage_actions)
Using your name and your password in the relevant boxes at the top of the page
You can use R as a calculator for example you can type 2 + 2
and get 4
2 + 2
Use the box below to calculate the product of 6 and 7.
6 * 7
gradethis::grade_code(correct = "6 * 7")
That's all good but how can I use powers? I see no way to enter a superscript. i.e. 2^2^
You can use the special symbol ^
to denote the power. I.e 2^2
means 2^2^
Remember that a square root is the inverse operation to the power of 2. So to calculate $\sqrt{2}$ in R you can simply type 2^(1/2)
. Note the use of brackets! 2^1/2
gives the wrong results because R first calculates 2^1
and then divised by 2
.
Another option to denote a square root is the function sqrt()
. I.e. sqrt(2)
# check code gradethis::grade_result( fail_if( ~ !all.equal(.result, ((6 * 7) + 4 - 7) / 2^8), gradethis::random_encourage()), pass_if(~ TRUE, gradethis::random_praise()), glue_correct = "{.message}" )
R uses some special 'numbers' to represent infinity or 'undefined'
Now, try to calculate 1/0, -1/0, and 0/0 and find out what is the result
=
or <-
to assign a value to a variable. I.e. var <- 42
creates a variable with name 'var' that holds the value 42.learnr::question( "Which of these are assignment operations? (Select ALL that apply)", answer('x == 6', correct = FALSE), answer('x <- 6', correct = TRUE), answer('x = 6', correct = TRUE), incorrect = "Give it another try. We use `=` or `<-` as assignment operators", random_answer_order = TRUE, allow_retry = TRUE, options = list(foobar = "Flag") )
Naming variables is a hard task. Ideally, variable names should be short to type easily, but informative so your code is more legible
Remember R is CASE SENSITIVE! John
is not the same as john
Variable names cannot begin with numbers or include spaces
Try assign 9 to a variable named 3john and see what happens
What do you think the following lines of R code will output at the end?
x <- 2 x <- x + 5
learnr::question("What is the value of x?", answer("x5", correct = FALSE), answer("7", correct = TRUE, message = gradethis::random_praise()), answer("0", correct = FALSE), allow_retry = TRUE, random_answer_order = TRUE )
We often have more than a single number, which we combine into a vector using
the function c()
, e.g.
c(184, 162, 145, 200, 178, 154, 172, 142)
As before with single values, we can assign the vector to a variable
heights_cm <- c(184, 162, 145, 200, 178, 154, 172, 142) heights_cm
Many operations in R are vectorised, meaning they apply to all elements of a vector, e.g.
heights_m <- heights_cm / 100 # convert cm to m heights_m
Notice that every element of the vector was divided by 100!
heights_m[5]
heights_m
has a length of 8. What will happen if we request the 10-th element that doesn't really exists?heights_m[10]
NA
which denotes a missing valuelearnr::question( "How do you select the 7-th value from the vector heights?", answer("heights(7)", correct = FALSE, message = gradethis::random_encourage()), answer("heights[7]", correct = TRUE, message = gradethis::random_praise()), answer("heights([7])", correct = FALSE, message = gradethis::random_encourage()), incorrect = "Give it another try. We use square brackets to select from a vector", random_answer_order = TRUE, allow_retry = TRUE, options = list(foobar = "Flag") )
How to select 1-st, 3-rd and 5-th element from the vector heights_m?
# solution code heights_m[c(1, 3, 5)]
# check code gradethis::grade_code()
Boils down to something being TRUE
or FALSE
\
x > y
asks: is x greater than y?\
x < y
asks: is x less than y?\
x == y
asks: is x equal to y?\
x >= y
asks: is x greater than or equal to y\
x <= y
asks: is x less than or equal to y?\
Note the different meaning of =
and ==
!!! The first assigns, the second checks equality
Basic examples:
5 < 10 3 > 5
There are three useful operators that help us combine simple logical statements\
&
means 'AND'. It returns TRUE
only if the expression in both sides is TRUE
\
|
means 'OR'. It returns TRUE
if any of the expression in both sides is TRUE
\
!
means 'NOT'. It converts TRUE
to FALSE
and vice versa\
Using these symbols we can combine logical statements, for example:
(5 < 10) & (3 < 5) (1 > 2) | (4 > 3) !(5 < 10) 5 != 10 # same as !(5 == 10)
learnr::question( "Evaluate the logical statement `(1 < 2) & (10 < 9)`", answer("TRUE", correct = FALSE, message = gradethis::random_encourage()), answer("FALSE", correct = TRUE, message = gradethis::random_praise()), incorrect = "Not quite right...", random_answer_order = TRUE, allow_retry = TRUE, options = list(foobar = "Flag") )
learnr::question( "Evaluate the logical statement `(1 < 2) | (10 < 9)`", answer("FALSE", correct = FALSE, message = gradethis::random_encourage()), answer("TRUE", correct = TRUE, message = gradethis::random_praise()), incorrect = "Not quite right...", random_answer_order = TRUE, allow_retry = TRUE, options = list(foobar = "Flag") )
learnr::question( "Evaluate the logical statement `!(1 < 2)`", answer("TRUE", correct = FALSE, message = gradethis::random_encourage()), answer("FALSE", correct = TRUE, message = gradethis::random_praise()), incorrect = "Not quite right...", random_answer_order = TRUE, allow_retry = TRUE, options = list(foobar = "Flag") )
We can use a logical vector to pick out elements of a vector. Let's try first with a logical vector that is the same length as the vector we want to partition.
heights_cm # to remind us its content logic_vec <- c(TRUE, FALSE, TRUE, FALSE, TRUE, FALSE, TRUE, FALSE) heights_cm[logic_vec]
We can simplify this further. Notice from above that our goal is to get every other element of the vector. When R uses vectorised operations and the vectors have unequal lengths, it silently recycles the shorter vector(s) to the length of the longest vector. For example:
logic_vec2 <- c(TRUE, FALSE) heights_cm[logic_vec2] # same as before because of recycling
R silently recycles logic_vec2
internaly to reach the length of heights_cm
by repeating logic_vec2
. So logic_vec2
silently becomes c(TRUE, FALSE, TRUE, FALSE, TRUE, FALSE, TRUE, FALSE)
when interacts with heights_cm
.
How to extract heights greater than 160cm?
Remember that heights_cm > 160
returns a logical vector
ind <- heights_cm > 160 ind
So then we can simply do
heights_cm[ind]
Or we can combine the 3 lines above into 1
heights_cm[heights_cm > 160]
Can you extract heights less than or equal to 180cm in one line?
heights_cm[heights_cm <= 180]
# check code gradethis::grade_code()
Click the submit button at the top of the page
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.