Hi folks!
I have a question - I have toy some data I collected on whether people could accurately identify skittle colours below, and I think I've got an answer to tidying this, but it involves an extra step using rename
.
I notes that it was quite similar to https://tidyr.tidyverse.org/articles/pivot.html#multiple-observations-per-row-1, but I couldn't quite get there.
I was wondering if anyone has some suggestions on how to improve this so I can do it all in one with pivot_longer
? I feel like I'm missing something.
Below is the reprex:
skittles_raw <- tibble::tribble(
~skittle_type, ~person_a, ~order_a, ~person_b, ~order_b, ~person_c, ~order_c,
1, "purple", 4, "red", 9, "purple", 6,
1, "red", 10, "green", 10, "red", 10,
2, "yellow", 2, "orange", 4, "orange", 1,
2, "orange", 5, "orange", 8, "green", 9,
3, "orange", 3, "orange", 2, "orange", 2,
3, "red", 7, "yellow", 7, "yellow", 5,
4, "purple", 8, "green", 1, "red", 3,
4, "yellow", 9, "red", 3, "yellow", 4,
5, "purple", 1, "purple", 5, "purple", 7,
5, "green", 6, "purple", 6, "purple", 8
)
library(tidyr)
library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
skittles_raw_not_quite <- skittles_raw %>%
pivot_longer(cols = -skittle_type,
names_to = c(".value", "person"),
names_sep = "_",
values_to = "choice",
values_drop_na = TRUE,
names_repair = "unique")
#> New names:
#> * person -> person...2
#> * person -> person...3
skittles_raw_not_quite
#> # A tibble: 30 x 4
#> skittle_type person...2 person...3 order
#> <dbl> <chr> <chr> <dbl>
#> 1 1 a purple 4
#> 2 1 b red 9
#> 3 1 c purple 6
#> 4 1 a red 10
#> 5 1 b green 10
#> 6 1 c red 10
#> 7 2 a yellow 2
#> 8 2 b orange 4
#> 9 2 c orange 1
#> 10 2 a orange 5
#> # … with 20 more rows
# I get what I want when I do this:
skittles_raw_not_quite %>%
rename(person = person...2,
choice = person...3)
#> # A tibble: 30 x 4
#> skittle_type person choice order
#> <dbl> <chr> <chr> <dbl>
#> 1 1 a purple 4
#> 2 1 b red 9
#> 3 1 c purple 6
#> 4 1 a red 10
#> 5 1 b green 10
#> 6 1 c red 10
#> 7 2 a yellow 2
#> 8 2 b orange 4
#> 9 2 c orange 1
#> 10 2 a orange 5
#> # … with 20 more rows
# but how do I get there without this extra step?
Created on 2020-07-22 by the reprex package (v0.3.0)
Also available at https://gist.github.com/njtierney/b40724d6a00a7a857d9fd641f9a9b568/edit