-1

I've got a list of participants that are listed multiple times in a column, I want them listed once. They have all completed an event a varying number of times. I need a column with the date for first attempt, second etc with NA if they haven't repeated.

 df
  participant  date
1 a            2020-01-02
2 b            2020-09-25
3 c            2020-09-29
4 d            2020-10-07
5 d            2020-10-07
6 d            2020-11-08
7 a            2020-10-12
8 b            2020-10-13

  participant  date1       event1  date2      event2 date3       event3
1 a            2020-01-02    1     2020-10-12  2      NA          NA
2 b            2020-09-25    1     2020-10-13  2      NA          NA
3 c            2020-09-29    1     NA          NA     NA          NA
4 d            2020-10-07    1     2020-11-07  2      2020-10-08  3  
Ian Campbell
  • 23,484
  • 14
  • 36
  • 57
aaa.hhh
  • 23
  • 4

1 Answers1

1

use this

library(dplyr)
library(tidyr)

df %>% group_by(participant) %>%
  mutate(event = row_number(),
         d3 = row_number()) %>%
  pivot_wider(id_cols = participant, names_from = d3, values_from = c(date, event))

# A tibble: 4 x 7
# Groups:   participant [4]
  participant date_1     date_2     date_3     event_1 event_2 event_3
  <chr>       <chr>      <chr>      <chr>        <int>   <int>   <int>
1 a           2020-01-02 2020-10-12 NA               1       2      NA
2 b           2020-09-25 2020-10-13 NA               1       2      NA
3 c           2020-09-29 NA         NA               1      NA      NA
4 d           2020-10-07 2020-10-07 2020-11-08       1       2       3
AnilGoyal
  • 25,297
  • 4
  • 27
  • 45