I have a data frame with 900,000 rows and 11 columns in R. The column names and types are as follows:
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 the data by those variables in the following order:
The order of levels are important here, i.e. they should be sorted by date first, and if there are identical dates, they should be sorted by mcode, so and so forth. How can I do that in R?
There is a function in R that you can use (called the sort function) to sort your data in either ascending or descending order. The variable by which sort you can be a numeric, string or factor variable. You also have some options on how missing values will be handled: they can be listed first, last or removed.
Sorting of a Vector in R Programming – sort() Function sort() function in R Language is used to sort a vector by its values. It takes Boolean value as argument to sort in ascending or descending order.
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 several vectors as arguments.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With