x <- 1:50
y <- 50:1

# solution using `base`
z <- (y - c(NA, x[1:(length(x)-1)]))/c(NA, NA, y[1:(length(y)-2)])

# solution with tidyverse (much prettier huh!)
z <- (y - lag(x, n = 1))/lag(y, n = 2)

# solution (be aware of the NA values)
my_sol <- sum(z, na.rm = TRUE)
my_answers <- make_random_answers(my_sol)

#check_answers(my_answers)

Question

Create a $z_i$ vector according to the following formula where $x_i = 1 ... 50$ and $y_i = 50 ... 1$. What is the sum of the elements of $z_i$? Tip: check out how the dplyr::lag function works.

$$ z_i=\frac{y_i - x_{i-1}}{y_{i-2}} $$

exams::answerlist(my_answers, markup = "markdown")

Solution


Meta-information

extype: schoice exsolution: r mchoice2string(c(TRUE, FALSE, FALSE, FALSE, FALSE), single = TRUE) exname: "numeric " exshuffle: TRUE



msperlin/afedR documentation built on Sept. 11, 2022, 9:49 a.m.