library(learnr)
library(gradethis)
knitr::opts_chunk$set(echo = FALSE)
gradethis_setup(
  pass.praise = TRUE, fail.hint = FALSE,
  fail.encourage = TRUE, maybe_code_feedback = FALSE)

Objects and assignments

Content quiz

quiz(
  question(
    "Which of the following commands is equivalend to `xx <- 2`?",
    answer('assign("xx", 2)', correct = TRUE),
    answer("xx -> 2"),
    answer("name('xx', 2)"),
    answer("assign(xx, 2)"), 
    allow_retry = TRUE, random_answer_order = TRUE
  ),
  question(
    "Which of the following statements about objects and names is correct?",
    answer("Every object can only have a unique name, just as every name must point to a unique object)!"),
    answer("While every object must have a unique name, each name can point to none, one, or several objects!"),
    answer("Names can point to multiple objects, and objects can have more than one name."),
    answer("An object can have many names, only every name must point to a unique object", 
           correct = TRUE), 
    allow_retry = TRUE, random_answer_order = TRUE
  ),
  question(
    "What can you do to remove all object-name assignment in your current R session?",
    answer("Restart the R session!", 
           correct = TRUE),
    answer("Call `rm(name)` for each assignment.", 
           correct = TRUE),
    answer("Click on the broom in the upper right environment panel in R-Studio", 
           correct = TRUE),
    answer("Call `rm(list=ls())` in the console.", 
           correct = TRUE),
    answer("Remove the assignment from the script and re-run the script from above."), 
    allow_retry = TRUE, random_answer_order = TRUE
    )
)

Coding exercise

Compute the following mathematical operations in R:

$$10+25$$


**Hint:** It might be a good idea to search for something like 'addition in R' in the web.
grade_result(
  pass_if(~identical(.result, 10+25))
)

$$50\cdot 2$$


**Hint:** It might be a good idea to search for something like 'multiplication in R' in the web.
grade_result(
  pass_if(~identical(.result, 50*2))
)

$$\frac{27}{4}$$


**Hint:** It might be a good idea to search for something like 'division in R' in the web.
grade_result(
  pass_if(~identical(.result, 27/4))
)

$$500-650$$


**Hint:** It might be a good idea to search for something like 'substraction in R' in the web.
grade_result(
  pass_if(~identical(.result, 500-650))
)

$$2^4$$


**Hint:** It might be a good idea to search for something like 'raise to power in R' in the web.
grade_result(
  pass_if(~identical(.result, 2**4))
)

$$\sqrt{3}\cdot 60$$


**Hint:** It might be a good idea to search for something like 'root in R' in the web.
grade_result(
  pass_if(~identical(.result, sqrt(3)*60))
)

$$50 \cdot (4+2)^3$$


**Hint:** It might be a good idea to search for something like 'calculator use r' in the web.
grade_result(
  pass_if(~identical(.result, 50*(4+2)**3))
)

Do the following chain computation in R. Assign a name to each intermediate result and then return the overall result.

Hint: avoid overwriting existing R functions such as c() since this can be confusing. Better use, e.g., names such as var_c instead.

\begin{align} a &= 10 + 25\ b &= a \cdot 40\ c &= b - 100\ d &= c \cdot 3\ e &= \sqrt{d}\ f &= \frac{e}{1000}\ g &= f^3 \end{align}


**Hint:** After assigning an intermediate result to a name you can call it via this name in the next calculation step. And don't forget to call the final result in the last line!
a_ <- 10 + 25
b_ <- a_ * 40
c_ <- b_ - 100
d_ <- c_ * 3
e_ <- sqrt(d_)
f_ <- e_ / 1000
g_ <- f_^3
g_
grade_result(
  pass_if(~identical(.result, 0.0002435549))
)


graebnerc/DataScienceExercises documentation built on April 11, 2025, 7:57 p.m.