Pipes

Question 1

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
x <- 1:20 %>% rep(2) %>% sum()

Question 2

Deconstruct the pipeline below back into separate commands.

# do not edit this chunk
lager <- LETTERS[c(18, 5, 7, 1, 12)] %>%
  rev() %>%
  paste(collapse = "")
regal <- LETTERS[c(18, 5, 7, 1, 12)]
reversed <- rev(regal)
lager <- paste(reversed, collapse = "") # make it into a string

Sensation Seeking

Questions 3-7 all have errors. Fix the errors in the code blocks below them.

Question 3

Load the data from sensation_seeking.csv.

ss <- read_csv(https://psyteachr.github.io/msc-data-skills/data/sensation_seeking.csv)
## Error: <text>:1:22: unexpected '/'
## 1: ss <- read_csv(https:/
##                          ^
ss <- read_csv("https://psyteachr.github.io/msc-data-skills/data/sensation_seeking.csv")

Question 4

Convert from wide to long format.

ss_long <- gather(ss, "question", "score")
## Warning: attributes are not identical across measure variables;
## they will be dropped
ss_long <- gather(ss, "question", "score", sss1:sss14)

Question 5

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

ss_sep <- ss_long %>%
  separate(question, domain, qnumber, sep = 3)
## Error in separate.data.frame(., question, domain, qnumber, sep = 3): object 'domain' not found
ss_sep <- ss_long %>%
  separate(question, c("domain", "qnumber"), sep = 3)

Question 6

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

ss_unite <- ss_sep %>%
  unite(id, user_id, "super_id", sep = "-")
## Unknown column `super_id`
ss_unite <- ss_sep %>%
  unite("super_id", id, user_id, sep = "-")

Question 7

Convert back to wide format.

ss_wide <- ss_unite %>%
  spreadr(qnumber, score)
## Error in spreadr(., qnumber, score): could not find function "spreadr"
ss_wide <- ss_unite %>%
  spread(qnumber, score)

Family Composition

Question 8

Load the dataset family_composition.csv.

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 <- read_csv("https://psyteachr.github.io/msc-data-skills/data/family_composition.csv") %>%
  gather("sibtype", "n", oldbro:twinsis) %>%
  separate(sibtype, c("sibage", "sibsex"), sep = -3)

Eye Descriptions

Question 9

Tidy the data from eye_descriptions.csv. 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 slashes, you would set the sep argument to "(~|\\/)+".

eyes <- read_csv("https://psyteachr.github.io/msc-data-skills/data/eye_descriptions.csv") %>%
  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

Answer Checks

You’ve made it to the end. Make sure you are able to knit this document to HTML. You can check your answers below in the knit document.

Question Answer
Question 1 correct
Question 2 correct
Question 3 correct
Question 4 correct
Question 5 correct
Question 6 correct
Question 7 correct
Question 8 correct
Question 9 correct