library(learnr) library(testwhat) knitr::opts_chunk$set(echo = FALSE) tutorial_options(exercise.timelimit = 60, exercise.checker=testwhat::testwhat_learnr) require(tidyverse) data(mtcars)
For the following examples you should use functions from dplyr
to extract the relevant pieces of data from the mtcars
data set:
head(mtcars)
Find the cars which get at least 20 miles per gallon (mpg
).
# tidy the penguins data mtcars %>% filter(mpg>=20) %>% arrange(mpg)
ex() %>% { check_error(.) check_function(., "filter", index=1) %>% check_result(.) %>% check_equal(.) }
Order the data in mtcars
by highest to lowest weight (wt
)
mtcars %>% arrange(desc(wt))
ex() %>%{ check_error(.) check_function(., "arrange") %>% check_result(.) %>% check_equal(., incorrect_msg="Did you sort the data in decreasing order?") }
Extract the rows from the mtcars
data set where the number of gears (gear
) is not equal to 4. Order the results by increasing horsepower (hp
)
mtcars %>% filter(gear!=4) %>% arrange(hp)
ex() %>%{ check_error(.) check_or(., {check_function(.,"filter") %>% check_result(.) %>% check_equal(.) check_function(.,"arrange") %>% check_result(.) %>% check_equal(.)}, override_solution_code(., "mtcars %>% arrange(hp) %>% filter(gear!=4)") %>% {check_function(.,"arrange") %>% check_result(.) %>% check_equal(.) check_function(.,"filter") %>% check_result(.) %>% check_equal(.)}) }
Extract all of the columns from mtcars
from hp
to vs
.
mtcars %>% select(hp:vs)
ex() %>%{ check_error(.) check_function(.,"select") %>% check_result(.) %>% check_equal(.) }
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.