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") 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))) test <- test %>% pivot_wider(names_from=Name, values_from=Grade)
penguins<- readr::read_csv("https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2020/2020-07-28/penguins.csv")
The data set penguins
is shown below:
penguins
In the box below, use the pivot_longer()
function to get this penguins data into untidy data format, so that the measurement type (columns bill_length_mm:body_mass_g
) is in its own column called "Measurement", and the observations for each measurement are in a column called "Observations".
# untidy the penguins data
# tidy the penguins data penguins %>% pivot_longer(bill_length_mm:body_mass_g, names_to="Measurement", values_to="Observations")
ex() %>% { check_error(.) check_function(., "pivot_longer") %>% { check_arg(., "cols") %>% check_equal(., eval=FALSE) check_arg(., "names_to") %>% check_equal(., eval=FALSE) check_arg(., "values_to") %>% 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))) test <- test %>% pivot_wider(names_from=Name, values_from=Grade)
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, and each student has one row of data. (Note that you will need to use two tidyr
functions here.) Student names should be in a column called "Name". Make sure to name the columns appropriately by adding "Assessment" to the assessment numbers. Store your result in a data frame called grades_tidy
.
# tidy the test data set
grades_tidy<- test %>% pivot_longer(Kim:Billy, names_to="Name", values_to="Grade")%>% pivot_wider(names_from=Assessment, values_from=Grade, names_prefix="Assessment")
ex() %>%{ check_error(.) check_function(., "pivot_longer") %>% { check_arg(., "names_to") %>% check_equal(., eval=FALSE, eq_fun=function(x,y) {stringr::str_extract(x, "[[:alpha:]]+" )==stringr::str_extract(y, "[[:alpha:]]+" )}) check_arg(., "values_to") %>% check_equal(., eval=FALSE, eq_function=function(x,y) {stringr::str_extract(x, "[[:alpha:]]+" )==stringr::str_extract(y, "[[:alpha:]]+" )}) } check_function(., "pivot_wider") %>% { check_arg(., "names_from") %>% check_equal(., eval=FALSE, eq_function=function(x,y) {stringr::str_extract(x, "[[:alpha:]]+" )==stringr::str_extract(y, "[[:alpha:]]+" )}) check_arg(., "values_from") %>% check_equal(., eval=FALSE, eq_function=function(x,y) {stringr::str_extract(x, "[[:alpha:]]+" )==stringr::str_extract(y, "[[:alpha:]]+" )}) check_arg(., "names_prefix") } #check_object(., "grades_tidy") %>% check_equal(.) }
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.