Sort and omit NAs in multiple columns of different length

I have a data frame with 563 columns.

Each column currently has rows of different length and NAs

For example:

col_1     col_2   col_3   ...   col_563  
"1"       "3"     "1"           NA
"3"       "4"     "3"           "2"
"2"       "1"     NA            "3"
NA        "2"     NA            "1" 
"4"       NA      "2"           NA

I want to sort all columns and omit the NAs so it looks like this:

col_1     col_2   col_3   ...   col_563  
"1"       "1"     "1"           "1"
"2"       "2"     "2"           "2"
"3"       "3"     "3"           "3"
"4"       "4"                   
                         

To first sort the columns, I tried:

df_sorted <-df_final[order(df_final[,1:563] ),]

But the program crashed. Any help would be appreciated, thank you!

>Solution :

your_df[] <- lapply(your_df, sort, na.last = TRUE)
your_df[is.na(your_df)] <- ''
  col_1 col_2 col_3 col_563
1     1     1     1       1
2     2     2     2       2
3     3     3     3       3
4     4     4              
5

Leave a Reply