library(learnr)
library(gradethis)
library(tidyverse)

knitr::opts_chunk$set(
  echo = FALSE,
  exercise.warn_invisible = FALSE
)

# enable code checking
tutorial_options(
  exercise.checker = grade_learnr,
  exercise.lines = 8,
  exercise.reveal_solution = TRUE
)

penguins_long <- palmerpenguins::penguins %>% 
  mutate(smaple = row_number()) %>% 
  pivot_longer(contains("_"),
               names_to = c("part", "measure" , "unit"),
               names_sep = "_")

penguins_long_simple <- palmerpenguins::penguins %>% 
  mutate(smaple = row_number()) %>% 
  pivot_longer(contains("_"))

Challenge 1

1a

Read in the penguins data set from file.

penguin_path <- palmerpenguins::path_to_file("penguins.csv" )
read.csv(__, header = TRUE)
penguin_path <- palmerpenguins::path_to_file("penguins.csv" )
read.csv(penguin_path, header = TRUE)
grade_code(
  correct = random_praise(),
  incorrect = random_encouragement()
)
Make sure you provide the object with the path saved in it to read.csv.

1b

Read in the penguins raw data set from file

penguin_path <- palmerpenguins::path_to_file(__)
read.csv(__, header = TRUE)
penguin_path <- palmerpenguins::path_to_file("penguins_raw.csv" )
read.csv(penguin_path, header = TRUE)
grade_code(
  correct = random_praise(),
  incorrect = random_encouragement()
)
Make sure you provide the object with the path saved in it to read.csv.
Did you change the file to look for to "penguins_raw.csv"?

1c

Read in the penguins data set from file and assign it to the object penguins

penguin_path <- palmerpenguins::path_to_file(__)
read.csv(__, header = TRUE)
penguin_path <- palmerpenguins::path_to_file("penguins.csv" )
penguins <- read.csv(penguin_path, header = TRUE)
grade_code(
  correct = random_praise(),
  incorrect = random_encouragement()
)
Make sure you provide the object with the path saved in it to read.csv.
Did you change the file to look for to "penguins.csv"?
Make sure you assign the output of `read.csv` to `penguins` using `<-`

Challenge 2

2a

Start by pivoting the penguins data so that all the bill measurements (starts with "bill") are in the same column.

penguins %>% 
  pivot_longer(_)
penguins %>% 
  pivot_longer(starts_with("bill"))
grade_code(
  correct = random_praise(),
  incorrect = random_encouragement()
)
Have you selected the columns using `starts_with()`?
Do not worry about column naming, just get the measures into a column, and the measure names into another. 

2b

Pivot longer all columns with an underscore.

penguins %>% 
  pivot_longer(_)
penguins %>% 
  pivot_longer(contains("_"))
grade_code(
  correct = random_praise(),
  incorrect = random_encouragement()
)
Have you selected the columns using `contains()`?
Do not worry about column naming, just get the measures into a column, and the measure names into another. 

2c

As mentioned, pivot_longer accepts tidy-selectors. Pivot longer all numerical columns.

penguins %>% 
  pivot_longer(_)
penguins %>% 
  pivot_longer(where(is.numeric))
grade_code(
  correct = random_praise(),
  incorrect = random_encouragement()
)
Do you remember the `where` and `is.numeric` functions?
Try using `where(is.numeric)`

Challenge 3

3a

Pivot longer all the bill measurements, and alter the names in one go, so that there are three columns named "part", "measure" and "unit" after the pivot.

penguins %>% 
  pivot_longer(_,
               names_to = c(_, _ , _),
               names_sep = _)
penguins %>% 
  pivot_longer(starts_with("bill"),
               names_to = c("part", "measure" , "unit"),
               names_sep = "_")
grade_code(
  correct = random_praise(),
  incorrect = random_encouragement()
)
Have you selected the columns using `starts_with()`?

3b

Pivot longer all the bill measurements, and use the names_prefix argument. Give it the string "bill_". What did that do?

penguins %>% 
  pivot_longer(_,
               names__ = "_")
penguins %>% 
  pivot_longer(starts_with("bill"),
               names_prefix = "bill_")
grade_code(
  correct = random_praise(),
  incorrect = random_encouragement()
)
be sure to use `names_prefix = "bill_"`

3c

Pivot longer all the bill measurements, and use the names_prefix, names_to and names_sep arguments. What do you need to change in names_to from the previous example to make it work now that we also use names_prefix?

penguins %>% 
  pivot_longer(starts_with("bill"),
               names_prefix = _,
               names_to = ,
               names_sep = _)
penguins %>% 
  pivot_longer(starts_with("bill"),
               names_prefix = "bill_",
               names_to = c("bill_measure" , "unit"),
               names_sep = "_")
grade_code(
  correct = random_praise(),
  incorrect = random_encouragement()
)
Now that the "bill_" prefix is removed, there are only two columns produced and not three!
To be clear about the column content, name the first pivoted column "bill_measure".

Challenge 4

4a

Turn the penguins_long_simple dataset back to its original state

penguins_long_simple %>% 
  pivot_wider(_ = _,
              _ = _)
penguins_long_simple %>% 
  pivot_wider(names_from = name,
              values_from = value)
grade_code(
  correct = random_praise(),
  incorrect = random_encouragement()
)
Make sure you spell all column names correctly

Challenge 5

5a

Turn the penguins_long dataset back to its original state

penguins_long %>% 
  pivot_wider(_ = _,
              _ = _,
              _ = _)
penguins_long %>% 
  pivot_wider(names_from = c("part", "measure", "unit"),
              names_sep = "_",
              values_from = value)
grade_code(
  correct = random_praise(),
  incorrect = random_encouragement()
)
Make sure you spell all column names correctly


Athanasiamo/swc.tidyverse documentation built on Dec. 17, 2021, 9:48 a.m.