--- title: 'Formative Exercise 04: MSc Data Skills Course' author: "Psychology, University of Glasgow" output: html_document --- ```{r setup, include=FALSE} # please do not alter this code chunk knitr::opts_chunk$set(echo = TRUE, message = FALSE, error = TRUE) library(tidyverse) # install the class package dataskills to access built-in data # devtools::install_github("psyteachr/msc-data-skills) # or download data from the website # https://psyteachr.github.io/msc-data-skills/data/data.zip ``` 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. ```{r untidy} # 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() ``` ```{r tidy} tidy <- NULL ``` ## Pivot functions The questions in this section all have errors. Fix the errors. ### Load data Load the dataset [dataskills::sensation_seeking](https://psyteachr.github.io/msc-data-skills/data/sensation_seeking.csv) as `ss`. ```{r data} ss <- read_csv(dataskills::sensation_seeking) ``` ### pivot_longer Convert from wide to long format. ```{r pivot-longer} ss_long <- ss %>% pivot_longer(names_to = "question", values_to = "score") %>% glimpse() ``` ### pivot_wider Convert back to wide format. Make sure `ss_wide` is the same as `ss`. ```{r pivot-wider} ss_wide <- ss_long %>% pivot_wider(question, score) %>% glimpse() ``` ## 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. ```{r gather} ss_long <- gather(ss, "question", "score") %>% glimpse() ``` ### separate Split the `question` column from `ss_long` into two columns: `domain` and `qnumber`. ```{r separate} ss_sep <- ss_long %>% separate(question, domain, qnumber, sep = 3) %>% glimpse() ``` ### 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". ```{r unite} ss_unite <- ss_sep %>% unite(id, user_id, "super_id", sep = "-") %>% glimpse() ``` ### spread Convert back to wide format. (N.B. the new question columns headers will just be numbers, not "sss#") ```{r spread} ss_wide <- ss_unite %>% spreadr(qnumber, score, ) %>% glimpse() ``` ## Pipes ### Connect with pipes Re-write the following sequence of commands into a single 'pipeline'. ```{r ex_pipe1} # 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 ``` ```{r pipe1} x <- NULL ``` ### Deconnect pipes Deconstruct the pipeline below back into separate commands. ```{r ex_pipe2} # do not edit this chunk lager <- LETTERS[c(18, 5, 7, 1, 12)] %>% rev() %>% paste(collapse = "") %>% print() ``` ```{r pipe2} lager <- NULL ``` ## Pivot vs tidy verbs Load the dataset [dataskills::family_composition](https://psyteachr.github.io/msc-data-skills/data/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). ### Use pivot functions ```{r family-pivot} family_pivot <- NULL ``` ### Use tidy verbs ```{r family-tidy} family_tidy <- NULL ``` ## Multiple steps Tidy the data from [dataskills::eye_descriptions](https://psyteachr.github.io/msc-data-skills/data/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 commas, you would set the `sep` argument to `"(~|,)+"`. ```{r eyes} eyes <- NULL ```