Let's say I have the following data.table in R :
library(data.table) DT = data.table(x=rep(c("b","a","c"),each=3), y=c(1,3,6), v=1:9)
I want to order it in two columns (e.g. columns x and v ). I used this:
DT[order(x,v)]
But now I want to sort it by x (in descending order) and have the following code:
DT[order(-x)] #Error in -x : invalid argument to unary operator
Therefore, I believe that this error is due to the fact that class(DT$x)=character . Could you give me any suggestion to solve this problem?
I know that I can use DT[order(x,decreasing=TRUE)] , but I want to know the syntax for sorting by several columns, using both methods (some decreasing, some increasing) at the same time.
Please note that if you use DT[order(-y,v)] , the result will be approved, but if you use DT[order(-x,v)] , an error appears. So my question is: how to solve this error?
string sorting r key order data.table
Nestorghh Sep 10 '12 at 14:30 2012-09-10 14:30
source share