0

I have a large dataset of health records that I am trying to get from long to wide format. Currently it is in this format.

eid code date
Person_1 123 2006-07-12
Person_1 1555 2006-07-12
Person_1 1989 2009-08-12
Person_2 1020 2008-09-12
Person_2 1220 2008-07-15
Person_2 12929 2020-07-22

I would like it in this format:

|eid | Code_1 | date_1 | code_2 | date_2 | code_3 | date_3|

and so on. I was struggling with this in reshaping.

Many thanks

zx8754
  • 52,746
  • 12
  • 114
  • 209
DW1310
  • 147
  • 7
  • 1
    Try `df %>% group_by(eid) %>% mutate(rn = row_number()) %>% pivot_wider(names_from = rn, values_from = c(code, date))` – benson23 May 26 '23 at 10:44

0 Answers0