What function in R to reorder a data.frame that contains missing data (NA)?

5

I have the following data.frame :

Linha   Data    Total    Med.2    DP.2   Med.3    DP.3   Med.4    DP.4
1       2009    1.4749      NA      NA      NA      NA      NA      NA
2       2010    2.9945  2.2347  1.0746      NA      NA      NA      NA
3       2011    2.9945  2.9945  0.0000  2.4880  0.8774      NA      NA
4       2012    3.0000  2.9973  0.0039  2.9963  0.0032  2.6160  0.7607
5       2013    2.9973  2.9986  0.0019  2.9973  0.0027  2.9966  0.0026
6       2014    2.9973  2.9973  0.0000  2.9982  0.0016  2.9973  0.0022

I need to reorder the data.frame lines according to the Med.2 column information. How can I perform this procedure using R ?

  

I tried to use the functions order of the base package and arrange of the package dplyr , but in both I got a wrong result.

    
asked by anonymous 01.11.2015 / 04:44

1 answer

5

Reading your data:

dat <- read.table(text = "Linha   Data    Total    Med.2    DP.2   Med.3    DP.3   Med.4    DP.4
                          1       2009    1.4749      NA      NA      NA      NA      NA      NA
                          2       2010    2.9945  2.2347  1.0746      NA      NA      NA      NA
                          3       2011    2.9945  2.9945  0.0000  2.4880  0.8774      NA      NA
                          4       2012    3.0000  2.9973  0.0039  2.9963  0.0032  2.6160  0.7607
                          5       2013    2.9973  2.9986  0.0019  2.9973  0.0027  2.9966  0.0026
                          6       2014    2.9973  2.9973  0.0000  2.9982  0.0016  2.9973  0.0022", header = TRUE)

Maybe you have forgotten the comma? The solutions are:

Growing order:

dat[order(dat$Med.2),]

Descending order:

dat[order(dat$Med.2, decreasing = TRUE),]

Using dplyr:

Ascending:

library(dplyr)
dat %>% arrange(Med.2)

Descending:

dat %>% arrange(desc(Med.2))
    
01.11.2015 / 14:30