0

I am trying to get my data frame to be more condense. I have two columns, school and color and would like to instead have all the colors for that school in separate columns.

sample data is as follows:

School    Color  
HS  orange  
HS black  
MS black  
MS purple  
MS white  
ES1 black  
ES2 green  
ES2 orange

I would instead like the data to look like:

School Color1 Color2 Color3  
HS orange black NA  
MS black purple white  
ES1 black NA NA  
ES2 green orange NA
www
  • 38,575
  • 12
  • 48
  • 84

1 Answers1

1

dplyr

library(dplyr)
library(tidyr)

dat %>%
  group_by(School) %>%
  mutate(lbl = paste0("Color", row_number())) %>%
  ungroup() %>%
  pivot_wider(names_from = lbl, values_from = Color)
# # A tibble: 4 x 4
#   School Color1 Color2 Color3
#   <chr>  <chr>  <chr>  <chr> 
# 1 HS     orange black  <NA>  
# 2 MS     black  purple white 
# 3 ES1    black  <NA>   <NA>  
# 4 ES2    green  orange <NA>  

data.table

(Note: I'm using magrittr solely to visually break out each step via the %>% operator. This is easily done without that just by data.table-chaining.)

library(magrittr) # just for demonstration, can be done without
library(data.table)

as.data.table(dat) %>%
  .[, lbl := paste0("Color", seq_len(.N)), by = "School"] %>%
  dcast(., School ~ lbl, value.var = "Color")
#    School Color1 Color2 Color3
# 1:    ES1  black   <NA>   <NA>
# 2:    ES2  green orange   <NA>
# 3:     HS orange  black   <NA>
# 4:     MS  black purple  white
r2evans
  • 141,215
  • 6
  • 77
  • 149
  • 1
    Alternative `tidyverse`: `dat %>% group_by(School) %>% summarise(Color = list(Color)) %>% unnest_wider(Color)` – Axeman Feb 19 '20 at 00:44
  • Slick, though you forgot the column names :-) – r2evans Feb 19 '20 at 00:56
  • 1
    Well that's more ugly: `df %>% group_by(School) %>% summarise(Color = list(setNames(Color, seq_len(n())))) %>% unnest_wider(Color, '')` – Axeman Feb 19 '20 at 01:09
  • what package is pivot_wider in? I have tidyr loaded and it can not find the function – theofficefan12 Feb 19 '20 at 01:11
  • `tidyr`, shown if you type in `pivot_wider` (no parens, just the function name) at the end with ``. Also, if you type `??pivot_wider` (and have `tidyr` installed even if not loaded/attached), then you should be shown at least two fuxxy-matching functions: `tidyr::pivot_wider` and `tidyr::pivot_wider_spec`. – r2evans Feb 19 '20 at 01:14
  • it gives me error: object 'pivot_wider' not found – theofficefan12 Feb 19 '20 at 01:16
  • They introduced it in [`tidyr-1.0.0`](https://github.com/tidyverse/tidyr/releases/tag/v1.0.0) in Sep 2019, check `packageVersion("tidyr")` and update if below that. – r2evans Feb 19 '20 at 01:25
  • (This can be done with `tidyr::spread` as well, but they're encouraging migration to the `pivot_*` verbs. \*shrug\*) – r2evans Feb 19 '20 at 01:37