Sort data in R

I have a data frame with 900,000 rows and 11 columns in R. The names and types of columns look like this:

column name: date / mcode / mname / ycode / yname / yissue  / bsent   / breturn / tsent   / treturn / csales
type:        Date / Char  / Char  / Char  / Char  / Numeric / Numeric / Numeric / Numeric / Numeric / Numeric

I want to sort data by these variables in the following order:

  • the date
  • Mcode
  • ycode
  • yissue

The order of the levels is important here, i.e. they must first be sorted by date, and if there are identical dates, they should be sorted by mcode, and so on. How can I do this in R?

+5
source share
4 answers

Perhaps something like this?

> df<- data.frame(a=rev(1:10), b=rep(c(2,1),5), c=rnorm(10))
> df
    a b           c
1  10 2 -0.85212079
2   9 1 -0.46199463
3   8 2 -1.52374565
4   7 1  0.28904717
5   6 2 -0.91609012
6   5 1  1.60448783
7   4 2  0.51249796
8   3 1 -1.35119089
9   2 2 -0.55497745
10  1 1 -0.05723538
> with(df, df[order(a, b, c), ])
    a b           c
10  1 1 -0.05723538
9   2 2 -0.55497745
8   3 1 -1.35119089
7   4 2  0.51249796
6   5 1  1.60448783
5   6 2 -0.91609012
4   7 1  0.28904717
3   8 2 -1.52374565
2   9 1 -0.46199463
1  10 2 -0.85212079

The order function can take multiple vectors as arguments.

+10
source

based on an earlier solution, here are two other approaches. the second approach requires plyr.

df.sorted = df[do.call(order, df[names(df)]),];
df.sorted = arrange(df, a, b, c) 
+8

, orderBy() doBy:

require(doBy)
sortedData <- orderBy(~date+mcode+ycode+yissue , data=unsortedData)

, , .

orderBy(). , " order() - , ".

, .

+4

: -c()

with(df, df[order(a, b, -c(myCharCol)), ])

with(df, df[order(a, b, c), c('a','b','x','y')])
+1

All Articles