How to compare 4 dates across 4 columns and create 4 new columns with the dates that are chronologically ordered in R dataframe

Viewed 22

New to R and to stack overflow. Thanks for your consideration of my question.

I have a dataframe which is wide and contains a row for each unique IDs and up to 4 dates in 4 columns for each ID. Not everyone has all four dates and the dates were not entered chronologically across the columns.

What I'm trying to do is create 4 new columns with the dates ordered chronologically for each row (ID). That is, the earliest date among the between 1 and 4 dates in the original columns I wish to appear in newcol1; then the next earliest date appears in the next column (newcol2) and so on.

So, for example, if the first ID has 4 dates in 4 columns:

2021-08-17, 2022-08-02, 2021-12-12, 2022-03-15

I want them to appear in chronological order in the 4 new columns:

2021-08-17, 2021-12-12, 2022-03-15, 2022-08-02

I know how to pull min and max, but that's not helpful for the middle values.

0 Answers
Related