Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading