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

Asked

Viewed 142 times

5

I got 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 lines of data.frame according to column information Med.2. How can I perform this procedure using the R?

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

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 forgot the comma? The solutions are:

Rising order:

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

Noncommittal:

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

Using dplyr:

Crescent:

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

Decreasing:

dat %>% arrange(desc(Med.2))

Browser other questions tagged

You are not signed in. Login or sign up in order to post.