Home > other >  R sort all columns with dates and NAs
R sort all columns with dates and NAs

Time:08-12

I have a data frame with 744 columns.

Each column currently contains multiple values of different length (in dates format using strptime)

To change the data to date format, I used

df <- as.data.frame(lapply(df, strptime, format="%Y-%m-%d %H:%M:%S"))

For example:

col_1            col_2               col_3       ...   col_744 
2021-01-01       2022-01-04          2022-01-08        2022-01-09 
2022-01-03       2021-01-01          2022-01-01        2022-01-01 
2022-03-01       2022-02-01          NA                NA
2022-02-01       NA                  NA                NA

I want to sort the dates in each column:

col_1            col_2               col_3       ...   col_744 
2021-01-01       2021-01-01          2022-01-01        2022-01-01 
2022-01-03       2022-01-04          2022-01-08        2022-01-09 
2022-02-01       2022-02-01          NA                NA
2022-03-01       NA                  NA                NA

I have tried:

df_sorted <- df[do.call(order, df), ]

but it did not work. I would also like to obtain all the columns without knowing the index. Thanks!

CodePudding user response:

You can use

df_sorted <- data.frame(lapply(df, sort, na.last = TRUE))

Since you are are sorting each column independently, you break the integrity of a row. If this is intended, I think you should use a list of "date" vectors for storage.

  • Related