# please do not alter this code chunk knitr::opts_chunk$set(echo = TRUE, message = FALSE, error = TRUE) library(tidyverse) library(reprores) # install the class package reprores to access built-in data # devtools::install_github("psyteachr/reprores-v2) # or download data from the website # https://psyteachr.github.io/reprores/data/data.zip
Edit the code chunks below and knit the document. You can pipe your objects to glimpse()
or print()
to display them.
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()
# your version can have different column names in a different order tidy <- tribble( ~id, ~df, ~t.value, ~p.value, ~conf.int.low, ~conf.int.high, "A", 26, -0.424, 0.6749, -0.444, 0.292, "B", 19, 0.754, 0.4600, -0.287, 0.610, "C", 19, 4.289, 0.0004, 0.374, 1.088 ) %>% print()
The questions in this section all have errors. Fix the errors.
Load the dataset reprores::sensation_seeking as ss
.
# has an error ss <- read_csv(reprores::sensation_seeking)
# corrects the error ss <- reprores::sensation_seeking ## alternatively ss <- read_csv("https://psyteachr.github.io/reprores/data/sensation_seeking.csv")
Convert from wide to long format.
# has an error ss_long <- ss %>% pivot_longer(names_to = "question", values_to = "score") %>% glimpse()
# corrects the error ss_long <- ss %>% pivot_longer(sss1:sss14, names_to = "question", values_to = "score") %>% glimpse()
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()
# corrects the error ss_wide <- ss_long %>% pivot_wider(names_from = question, values_from = score) %>% glimpse()
The questions in this section all have errors. Fix the errors.
Use the gather()
function to convert ss
from wide to long.
# has an error ss_long <- gather(ss, "question", "score") %>% glimpse()
# corrects the error ss_long <- gather(ss, "question", "score", sss1:sss14) %>% glimpse()
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()
# corrects the error ss_sep <- ss_long %>% separate(question, c("domain", "qnumber"), sep = 3) %>% glimpse()
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()
# corrects the error ss_unite <- ss_sep %>% unite("super_id", id, user_id, sep = "-") %>% glimpse()
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()
# corrects the error ss_wide <- ss_unite %>% spread(qnumber, score) %>% glimpse()
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
x <- 1:20 %>% rep(2) %>% sum() %>% print()
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()
regal <- LETTERS[c(18, 5, 7, 1, 12)] reversed <- rev(regal) lager <- paste(reversed, collapse = "") # make it into a string lager
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).
family_pivot <- reprores::family_composition %>% pivot_longer(cols = oldbro:twinsis, names_to = c("sibage", "sibsex"), names_sep = -3, values_to = "n") %>% glimpse()
family_tidy <- reprores::family_composition %>% gather("sibtype", "n", oldbro:twinsis) %>% separate(sibtype, c("sibage", "sibsex"), sep = -3) %>% glimpse()
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 <- reprores::eye_descriptions %>% gather("face_id", "description", t1:t50) %>% separate(description, c("d1", "d2", "d3", "d4"), sep = "(,|;|\\/)+", fill = "right") %>% gather("desc_n", "description", d1:d4) %>% filter(!is.na(description)) %>% # gets rid of rows with no description glimpse()
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.