library(learnr)
library(testwhat)
knitr::opts_chunk$set(echo = FALSE)
tutorial_options(exercise.timelimit = 60, exercise.checker=testwhat::testwhat_learnr)

require(tidyverse)
data(mtcars)
penguins<- readr::read_csv("https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2020/2020-07-28/penguins.csv")

For the following examples you should use functions from dplyr to carry out the relevant transformations on the mtcars data set:

head(mtcars)

Exercise 1

The horse-power data in the mtcars data set are skewed:

hist(mtcars$hp)

Add a new column to the data set, which contains the square root of hp


# tidy the penguins data
mtcars %>% mutate(sqrt_hp=sqrt(hp))
ex() %>% {
  check_error(.)
  check_function(., "mutate") %>% check_result(.) %>% check_equal(.)
}

Exercise 2

Standardise the mpg column using the standard deviation. Distinguish between automatic and manual transmissions (am)


mtcars %>% group_by(am) %>% mutate(mpg_std = mpg/sd(mpg))
ex() %>%{
  check_error(.)
  check_function(., "group_by") 
  check_function(., "mutate")%>% check_result(.) %>% check_equal(., incorrect_msg="Did you group by transmission type before using mutate? Try checking your calculation - you should divide mpg by its standard deviation.")
}

Exercise 3

Using the penguins data:

penguins

calculate the log bill length and bill depth by species.


penguins %>% mutate(across(starts_with("bill_"), log))
ex() %>%{
  check_error(.) 
  check_function(., "mutate")%>% check_result(.) %>% check_equal(., incorrect_msg="Did you take the log of the bill related columns correctly?")
}


kate-pyper/MM916ProgrammingExercises documentation built on Oct. 15, 2020, 10:40 p.m.