library(learnr) library(testwhat) knitr::opts_chunk$set(echo = FALSE) tutorial_options(exercise.timelimit = 60, exercise.checker=testwhat::testwhat_learnr) require(tidyverse) penguins<- readr::read_csv("https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2020/2020-07-28/penguins.csv") penguins <- penguins %>% mutate(Sample_Number=1:nrow(penguins)) %>% pivot_longer(bill_length_mm:body_mass_g, names_to="Measurement", values_to="Observations") set.seed(123) test <- tibble(Name=rep(c("Kim","Louise","Susan","Johnny","Scott","Stephen","David","Catriona","Rebecca","Donna","Lynne","John","Billy"), each=5), Assessment=rep(1:5, times=13), Grade=c(sample(70:90, 5, replace = TRUE),sample(60:80, 5, replace = TRUE), sample(75:85, 5, replace = TRUE), sample(50:60, 5, replace = TRUE),sample(40:60, 5, replace = TRUE), sample(80:100, 5, replace = TRUE), sample(60:70, 5, replace = TRUE), sample(35:50, 5, replace = TRUE), sample(45:70, 5, replace = TRUE), sample(80:100, 5, replace = TRUE), sample(48:62, 5, replace = TRUE), sample(55:75, 5, replace = TRUE), sample(60:70, 5, replace = TRUE)))
penguins<- readr::read_csv("https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2020/2020-07-28/penguins.csv") penguins <- penguins %>% mutate(Sample_Number=1:nrow(penguins)) %>% pivot_longer(bill_length_mm:body_mass_g, names_to="Measurement", values_to="Observations")
The data set penguins
has been transformed into the following format:
penguins
In the box below, use the pivot_wider()
function to get the pre-loaded penguins data into tidy data format, where each measurement is in its own column.
# tidy the penguins data
# tidy the penguins data penguins %>% pivot_wider(names_from=Measurement, values_from=Observations)
ex() %>% { check_error(.) check_function(., "pivot_wider", index=1) %>% { check_arg(., "names_from") %>% check_equal(., eval=FALSE) check_arg(., "values_from") %>% check_equal(., eval=FALSE) } }
set.seed(123) test <- tibble(Name=rep(c("Kim","Louise","Susan","Johnny","Scott","Stephen","David","Catriona","Rebecca","Donna","Lynne","John","Billy"), each=5), Assessment=rep(1:5, times=13), Grade=c(sample(70:90, 5, replace = TRUE),sample(60:80, 5, replace = TRUE), sample(75:85, 5, replace = TRUE), sample(50:60, 5, replace = TRUE),sample(40:60, 5, replace = TRUE), sample(80:100, 5, replace = TRUE), sample(60:70, 5, replace = TRUE), sample(35:50, 5, replace = TRUE), sample(45:70, 5, replace = TRUE), sample(80:100, 5, replace = TRUE), sample(48:62, 5, replace = TRUE), sample(55:75, 5, replace = TRUE), sample(60:70, 5, replace = TRUE)))
A data set containing student grades across five different assessments has been loaded into an R session. The data is stored in a data frame called test
and is shown below:
test
In the box below, get the data into tidy format, where each assessment forms a column. Make sure to name the columns appropriately by adding "Assessment" to the assessment numbers:
# tidy the test data set
test %>% pivot_wider(names_from=Assessment, values_from=Grade, names_prefix="Assessment")
ex() %>%{ check_error(.) check_function(., "pivot_wider") %>% { check_arg(., "names_from") %>% check_equal(., eval=FALSE, eq_fun=function(x,y) {stringr::str_extract(x, "[[:alpha:]]+" )==stringr::str_extract(y, "[[:alpha:]]+" )}) check_arg(., "values_from") %>% check_equal(., eval=FALSE, eq_fun=function(x,y) {stringr::str_extract(x, "[[:alpha:]]+" )==stringr::str_extract(y, "[[:alpha:]]+" )}) check_arg(., "names_prefix") %>% check_equal(., eq_fun=function(x, y){tolower(x)==tolower(y)}) } }
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.