0

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.

Lstar
  • 31
  • 4
  • 2
    Does this answer your question? [How can I spread repeated measures of multiple variables into wide format?](https://stackoverflow.com/questions/29775461/how-can-i-spread-repeated-measures-of-multiple-variables-into-wide-format) – Dylan_Gomes Sep 28 '20 at 21:08
  • 1
    Please see https://stackoverflow.com/questions/29775461/how-can-i-spread-repeated-measures-of-multiple-variables-into-wide-format and https://stackoverflow.com/questions/30592094/r-spreading-multiple-columns-with-tidyr – Dylan_Gomes Sep 28 '20 at 21:08

1 Answers1

2

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     
Ben
  • 20,394
  • 4
  • 17
  • 35