I need to convert the event-level dataset to the patient-level data i.e. convert a long dataset to a wider one based on deidnum as a key variable. Additionally, I want to create columns for each produced event and its event time. In case the same patient has more than one event, consider the earliest event time.
Here is a similar data sample and my code:
df <- read.table(text = "deidnum,eventc,EVENTDT,MI_COMPLICATED
325107,MI,21,1
325107,New Rose Dyspnea Scale 2 or more,1468,NA
418351,New Rose Dyspnea Scale 2 or more,207,NA
839172,New Rose Dyspnea Scale 2 or more,1060,NA
839172,New Rose Dyspnea Scale 2 or more,1718,NA
1487422,MI,990,0
1487422,DEATH,1113,NA
1511165,MI,424,0
1511165,MI,608,1
1511165,New Rose Dyspnea Scale 2 or more,721,NA
", sep = ",", header = TRUE)
library(reshape2)
wide.df <- dcast(df, deidnum ~ eventc)
wide.df
The current output
deidnum DEATH MI New Rose Dyspnea Scale 2 or more
1 325107 0 1 1
2 418351 0 0 1
3 839172 0 0 2
4 1487422 1 1 0
5 1511165 0 2 1
Any advice will be greatly appreciated.

A
tidyverseworkflow:Note:
unused_fn = firstis for grouping by theid_colscolumns (deidnum) then summarizing the unused columns (MI_COMPLICATED) usingfirst()(assuming it has been ordered byEVENTDT).