Search code examples
rdplyrtidyrreshape2spread

R data long to wide with multiple output columns/values


Struggling with how to word this so I'm not finding great results via search. Example input:

cust_id  make  part  price  color
1        jeep  wheel 10     gray
1        jeep  door  5      blue
1        jeep  seat  20     brown
2        ford  wheel 12     gray
2        ford  door  8      red
2        ford  seat  25     brown

Desired output:

cust_ID  make  wheel_price  wheel_color  door_price  door_color  seat_price  seat_color
1        jeep  10           gray         5           blue        20          brown
2        ford  12           gray         8           red         25          brown

Originally was using spread() but ran into issues with the string values. Switched to pivot_wider() but still not able to get multiple output columns/values for each unique part. Thanks in advance!


Solution

  • you have to use pivot_wider:

    library(dplyr)
    library(tidyr)
    
    df %>% 
     tidyr::pivot_wider(names_from = part, names_glue = "{part}_{.value}", values_from = c(price, color)) %>%
     dplyr::select(1, 2, sort(current_vars()))
    
      cust_id make  door_color door_price seat_color seat_price wheel_color wheel_price
        <int> <chr> <chr>           <int> <chr>           <int> <chr>             <int>
    1       1 jeep  blue                5 brown              20 gray                 10
    2       2 ford  red                 8 brown              25 gray                 12