0

I have a large dataset with three different variables: ID, admission date, discharge date.

ID      Admit_Date     Discharge_date
1       2015-1-1       2015-3-9
2       2015-1-9       2015-4-10
2       2015-8-10      2015-11-2

Currently the dataset is in long format and I want to make it a wide dataset with each row corresponding to one ID. Since there are hundreds of different dates, I do not want the values to be column names as that would result in potentially hundreds of columns. I want there to be multiple admit/discharge columns so that it shows like "admit_date_1", "discharge_date_1", "admit_date_2", etc.

ID      Admit_Date_1    Discharge_date_1     Admit_Date_2     Discharge_date_2
1       2015-1-1       2015-3-9              NA               NA
2       2015-1-9       2015-4-10             2015-8-10        2015-11-2

I have tried using the dcast and melt functions in R but cannot get it to look like this above. Any help or guidance is appreciated! -J

Axeman
  • 32,068
  • 8
  • 81
  • 94
Joshua
  • 1
  • 1

1 Answers1

0

In base R you could do:

reshape(transform(df,time=ave(ID,ID,FUN=seq_along)),dir = "wide",idvar = "ID",sep = "_")
  ID Admit_Date_1 Discharge_date_1 Admit_Date_2 Discharge_date_2
1  1     2015-1-1         2015-3-9         <NA>             <NA>
2  2     2015-1-9        2015-4-10    2015-8-10        2015-11-2
Onyambu
  • 67,392
  • 3
  • 24
  • 53