Edit the code chunks below and knit the document. You can pipe your objects to glimpse() or print() to display them.

Tidy data

The following data table is not tidy. Use tibble() or tribble()to manually create the tidy version of this table.

# do not edit this chunk
untidy <- tribble(
  ~id, ~stats, ~p.value, ~conf.int,
  "A", "t(26) = -0.424", 0.6749,  "[-0.444, 0.292]",
  "B", "t(19) =  0.754", 0.4600,  "[-0.287, 0.610]",
  "C", "t(19) =  4.289", 0.0004,  "[ 0.374, 1.088]"
) %>% print()
## # A tibble: 3 x 4
##   id    stats          p.value conf.int       
##   <chr> <chr>            <dbl> <chr>          
## 1 A     t(26) = -0.424  0.675  [-0.444, 0.292]
## 2 B     t(19) =  0.754  0.46   [-0.287, 0.610]
## 3 C     t(19) =  4.289  0.0004 [ 0.374, 1.088]
# your version can have different column names in a different order
tidy <- NULL

Pivot functions

The questions in this section all have errors. Fix the errors.

Load data

Load the dataset reprores::sensation_seeking as ss.

# has an error
ss <- read_csv(reprores::sensation_seeking)
## Error: `file` must be a string, raw vector or a connection.
# corrects the error
ss <- NULL

pivot_longer

Convert from wide to long format.

# has an error
ss_long <- ss %>%
  pivot_longer(names_to = "question", 
               values_to = "score") %>%
  glimpse()
## Error in UseMethod("pivot_longer"): no applicable method for 'pivot_longer' applied to an object of class "NULL"
# corrects the error
ss_long <- NULL

pivot_wider

Convert back to wide format. Make sure ss_wide is the same as ss.

# has an error
ss_wide <- ss_long %>%
  pivot_wider(question, score) %>%
  glimpse()
## Error in UseMethod("pivot_wider"): no applicable method for 'pivot_wider' applied to an object of class "NULL"
# corrects the error
ss_wide <- NULL

Tidy verbs

The questions in this section all have errors. Fix the errors.

gather

Use the gather() function to convert ss from wide to long.

# has an error
ss_long <- gather(ss, "question", "score") %>%
  glimpse()
## Error in UseMethod("gather"): no applicable method for 'gather' applied to an object of class "NULL"
# corrects the error
ss_long <- NULL

separate

Split the question column from ss_long into two columns: domain and qnumber.

# has an error
ss_sep <- ss_long %>%
  separate(question, domain, qnumber, sep = 3) %>%
  glimpse()
## Error in UseMethod("separate"): no applicable method for 'separate' applied to an object of class "NULL"
# corrects the error
ss_sep <- NULL

unite

Put the id and user_id columns together into a new column named super_id. Make it in a format like “id-user_id”.

# has an error
ss_unite <- ss_sep %>%
  unite(id, user_id, "super_id", sep = "-") %>%
  glimpse()
## Error in UseMethod("unite"): no applicable method for 'unite' applied to an object of class "NULL"
# corrects the error
ss_unite <- NULL

spread

Convert back to wide format. (N.B. the new question columns headers will just be numbers, not “sss#”)

# has an error
ss_wide <- ss_unite %>%
  spreadr(qnumber, score, ) %>%
  glimpse()
## Error in spreadr(., qnumber, score, ): could not find function "spreadr"
# corrects the error
ss_wide <- NULL

Pipes

Connect with pipes

Re-write the following sequence of commands into a single ‘pipeline’.

# do not edit this chunk
x <- 1:20      # integers from 1:20
y <- rep(x, 2) # then repeat them twice
z <- sum(y)    # and then take the sum
z
## [1] 420
x <- NULL

Deconnect pipes

Deconstruct the pipeline below back into separate commands.

# do not edit this chunk
lager <- LETTERS[c(18, 5, 7, 1, 12)] %>%
  rev() %>%
  paste(collapse = "") %>%
  print()
## [1] "LAGER"
lager <- NULL

Pivot vs tidy verbs

Load the dataset reprores::family_composition.

The columns oldbro through twinsis give the number of siblings of that age and sex. Put this into long format and create separate columns for sibling age (sibage = old, young, twin) and sex (sibsex = bro, sis).

Use pivot functions

family_pivot <- NULL

Use tidy verbs

family_tidy <- NULL

Multiple steps

Tidy the data from reprores::eye_descriptions. This dataset contains descriptions of the eyes of 50 people by 220 raters (user_id). Some raters wrote more than one description per face (maximum 4), separated by commas, semicolons, or slashes.

Create a dataset with separate columns for face_id, description, and description number (desc_n).

Hint: to separate a string by tildes or commas, you would set the sep argument to "(~|,)+".

eyes <- NULL