How to sort a data.frame by columns in R?

5

Suppose a data.frame with numeric values and strings:

 set.seed(1)
dados <- data.frame(w=rep(c("A", "B"), 2), x= rep(c("D", "C"), 2), y= rnorm(4), z=rnorm(4),
                    stringsAsFactors=FALSE)
dados
 w x          y          z
1 A D -0.6264538  0.3295078
2 B C  0.1836433 -0.8204684
3 A D -0.8356286  0.4874291
4 B C  1.5952808  0.7383247

How do I sort the date.frame by a column? And how do I sort with more than one column, and each with different orders (some in ascending order, others in descending order)?

    
asked by anonymous 17.02.2014 / 18:51

3 answers

5

To do this right in order , just enter multiple parameters.

set.seed(1)
dados <- data.frame(w=rep(c("A", "B"), 2), x= rep(c("D", "C"), 2), y= rnorm(4), z=rnorm(4),
                    stringsAsFactors=FALSE)
dados[order(dados$w, dados$x, dados$y, decreasing=c(TRUE, FALSE, TRUE)), ] # No caso, ordena de acordo por w em ordem alfabética inversa, x em ordem alfabética e y em ordem decrescente. Se quiser que tudo seja na mesma ordem, basta um TRUE ou FALSE, que valerá para todos.

  w x          y          z
4 B C  1.5952808  0.7383247
2 B C  0.1836433 -0.8204684
1 A D -0.6264538  0.3295078
3 A D -0.8356286  0.4874291
    
17.02.2014 / 20:06
5

You can also use the plyr package for sorting.

library(plyr)
arrange(dados,desc(z))
  w x          y          z
1 B C  1.5952808  0.7383247
2 A D -0.8356286  0.4874291
3 A D -0.6264538  0.3295078
4 B C  0.1836433 -0.8204684

The arrange also works for text without needing the auxiliary function xtfrm

arrange(dados,desc(w),z)
  w x          y          z
1 B C  0.1836433 -0.8204684
2 B C  1.5952808  0.7383247
3 A D -0.6264538  0.3295078
4 A D -0.8356286  0.4874291

Another interesting option is to use the sqldf package. The sql default is ascending order.

library(sqldf)
sqldf("SELECT w,x,y,z 
      FROM dados
      Order BY x, desc y")
  w x          y          z
1 B C  1.5952808  0.7383247
2 B C  0.1836433 -0.8204684
3 A D -0.6264538  0.3295078
4 A D -0.8356286  0.4874291
    
17.02.2014 / 19:54
4

You can use the order function. To sort the data.frame by column z in descending order, for example:

dados[order(dados$z, decreasing=TRUE),]
  w x          y          z
4 B C  1.5952808  0.7383247
3 A D -0.8356286  0.4874291
1 A D -0.6264538  0.3295078
2 B C  0.1836433 -0.8204684

To sort differently for each column, there are two points to note. If the columns are numeric, you can put one less before the vector to invert the direction of this column, for example, order(dados$z, -dados$y, decreasing=TRUE) sorts in decreasing order in z then in ascending order in y (which in this case makes no difference as there are no draws in z).

With columns of characters you must use an auxiliary function xtfrm . For example, to sort the data "decreasingly" (alphabetically) in the w vector and then increasing in the z vector:

dados[order(-xtfrm(dados$w), dados$z),]
  w x          y          z
2 B C  0.1836433 -0.8204684
4 B C  1.5952808  0.7383247
1 A D -0.6264538  0.3295078
3 A D -0.8356286  0.4874291
    
17.02.2014 / 19:09