How to sort a column-by-column data.frame in R?

Asked

Viewed 17,265 times

6

Suppose a data.frame with numerical 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 data.frame by a column? And how do I order with more than one column, and each with different orders (some in ascending order, others in descending order)?

3 answers

8

You can also use the package plyr for the ordination.

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

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 default sql 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

7


You can do it right on order, simply 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
  • 1

    For you to see how you can always learn even in simple things, did not know that the decreasing accepted vectors!

5

You can use the function order. To sort the date.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 be observed. If the columns are numerical, you can put a minus before the vector to reverse the direction of this column, for example, order(dados$z, -dados$y, decreasing=TRUE) organizes in descending order in z and then in ascending order in y (which in this case makes no difference as there are no draws in z).

Already with columns of characters it is necessary to use an auxiliary function xtfrm. For example, to sort the data "decreasing" (alphabetically) in the vector w and then increasing in the vector z:

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

Browser other questions tagged

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