Lstar
Lstar

Reputation: 56

Long to wide conversion using tidyr for grouped data

I'm a rookie trying to figure out the best way to do a long-wide conversion using tidyr.

My data looks like this:

   ID  Case  Case_date
    1   A   1-Sep
    1   B   2-Sep
    1   C   3-Sep
    2   D   4-Sep
    3   E   5-Sep
    3   F   6-Sep

I want it to look like this:

ID  Case_1  Case_1_date  Case_2  Case_2_date    Case_3  Case_3_date
1    A      1-Sep        B       2-Sep          C       3-Sep
2    D      4-Sep        NULL    NULL           NULL    NULL
3    E      5-Sep        F       6-Sep          NULL    NULL

Any help is appreciated! I think pivot_wider is part of the solution for me here but I'm not quite getting it.

Upvotes: 0

Views: 55

Answers (1)

Ben
Ben

Reputation: 30474

With tidyverse you can do the following. Enumerate the cases for each ID and then pivot_wider to put into wide format.

library(tidyverse)

df %>%
  group_by(ID) %>%
  mutate(N = row_number()) %>%
  pivot_wider(id_cols = ID, names_from = N, values_from = c(Case, Case_date))

Output

     ID Case_1 Case_2 Case_3 Case_date_1 Case_date_2 Case_date_3
  <dbl> <chr>  <chr>  <chr>  <chr>       <chr>       <chr>      
1     1 A      B      C      1-Sep       2-Sep       3-Sep      
2     2 D      NA     NA     4-Sep       NA          NA         
3     3 E      F      NA     5-Sep       6-Sep       NA     

Upvotes: 2

Related Questions