```{=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)

R as a calculator

Remember to login!

Using your name and your password in the relevant boxes at the top of the page

Simple maths

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.


**Hint:** We need to calculate 6 times 7. Therefore `6 * 7` is the correct answer.
6 * 7
gradethis::grade_code(correct = "6 * 7")

Using powers

Using mathematical expressions


**Hint:** All you need is to type the expression from the question into the box.
# 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}"
)

Other 'numbers' to look out for


Assignment

Assignment operators

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")
)

Things to remember when naming variables


Variables task

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
)

Vectors

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!

Selection

Syntax

heights_m[5]
heights_m[10]

Selection task 1

learnr::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")
)

Selection task 2

How to select 1-st, 3-rd and 5-th element from the vector heights_m?


**Hint:** Remember that the 1-st, 3-rd and 5-th element is the vector `c(1, 3, 5)`
# solution code
heights_m[c(1, 3, 5)]
# check code
gradethis::grade_code()

Logic

Simple logical statements

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

More logic operators

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)

Logic task

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")
)

Logic & selection

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.

Logic & selection (c'nued)

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]

Logic & selection task

Can you extract heights less than or equal to 180cm in one line?


**Hint:** Try to modify the code from the previous example `heights_cm[heights_cm > 160]`
heights_cm[heights_cm <= 180]
# check code
gradethis::grade_code()

Remember to submit your answers!

Click the submit button at the top of the page



ChristK/CKteachR documentation built on Dec. 31, 2020, 10:59 a.m.