Skip to main content
Notice added Recommended answer in R Language by Sotos
Notice removed Recommended answer in R Language by Sotos
Notice added Recommended answer in R Language by Sotos
deleted 1 character in body
Source Link
Mus
  • 7.6k
  • 25
  • 93
  • 144

You can use the order() function directly without resorting to add-on tools -- see this simpler answer which uses a trick right from the top of the example(order) code:

R> dd[with(dd, order(-z, b)), ] b x y z 4 Low C 9 2 2 Med D 3 1 1 Hi A 8 1 3 Hi A 9 1 

Edit some 2+ years later: It was just asked how to do this by column index. The answer is to simply pass the desired sorting column(s) to the order() function:

R> dd[ orderdd[order(-dd[,4], dd[,1]), ] b x y z 4 Low C 9 2 2 Med D 3 1 1 Hi A 8 1 3 Hi A 9 1 R> 

rather than using the name of the column (and with() for easier/more direct access).

You can use the order() function directly without resorting to add-on tools -- see this simpler answer which uses a trick right from the top of the example(order) code:

R> dd[with(dd, order(-z, b)), ] b x y z 4 Low C 9 2 2 Med D 3 1 1 Hi A 8 1 3 Hi A 9 1 

Edit some 2+ years later: It was just asked how to do this by column index. The answer is to simply pass the desired sorting column(s) to the order() function:

R> dd[ order(-dd[,4], dd[,1]), ] b x y z 4 Low C 9 2 2 Med D 3 1 1 Hi A 8 1 3 Hi A 9 1 R> 

rather than using the name of the column (and with() for easier/more direct access).

You can use the order() function directly without resorting to add-on tools -- see this simpler answer which uses a trick right from the top of the example(order) code:

R> dd[with(dd, order(-z, b)), ] b x y z 4 Low C 9 2 2 Med D 3 1 1 Hi A 8 1 3 Hi A 9 1 

Edit some 2+ years later: It was just asked how to do this by column index. The answer is to simply pass the desired sorting column(s) to the order() function:

R> dd[order(-dd[,4], dd[,1]), ] b x y z 4 Low C 9 2 2 Med D 3 1 1 Hi A 8 1 3 Hi A 9 1 R> 

rather than using the name of the column (and with() for easier/more direct access).

Rollback to Revision 4
Source Link
Richie Cotton
  • 121.7k
  • 47
  • 254
  • 371

You can use the order() function directly without resorting to add-on tools -- see this simpler answer which uses a trick right from the top of the example(order) code:

R> dd[with(dd, order(-z, b)), ] b x y z 4 Low C 9 2 2 Med D 3 1 1 Hi A 8 1 3 Hi A 9 1 

Edit some 2+ years later: It was just asked how to do this by column index. The answer is to simply pass the desired sorting column(s) to the order() function:

R> dd[orderdd[ order(-dd[,4], dd[,1]), ] b x y z 4 Low C 9 2 2 Med D 3 1 1 Hi A 8 1 3 Hi A 9 1 R> 

rather than using the name of the column (and with() for easier/more direct access).


If you use minus with a character column, you get an error:

dd$x <- as.character(dd$x) dd[order(-dd[,2], dd[,1]), ] ## Error in -dd[, 2] : invalid argument to unary operator 

In this case you need to wrap the argument in xtfrm.

dd[order(-xtfrm(dd[,2]), dd[,1]), ] 

You can use the order() function directly without resorting to add-on tools -- see this simpler answer which uses a trick right from the top of the example(order) code:

R> dd[with(dd, order(-z, b)), ] b x y z 4 Low C 9 2 2 Med D 3 1 1 Hi A 8 1 3 Hi A 9 1 

Edit some 2+ years later: It was just asked how to do this by column index. The answer is to simply pass the desired sorting column(s) to the order() function:

R> dd[order(-dd[,4], dd[,1]), ] b x y z 4 Low C 9 2 2 Med D 3 1 1 Hi A 8 1 3 Hi A 9 1 R> 

rather than using the name of the column (and with() for easier/more direct access).


If you use minus with a character column, you get an error:

dd$x <- as.character(dd$x) dd[order(-dd[,2], dd[,1]), ] ## Error in -dd[, 2] : invalid argument to unary operator 

In this case you need to wrap the argument in xtfrm.

dd[order(-xtfrm(dd[,2]), dd[,1]), ] 

You can use the order() function directly without resorting to add-on tools -- see this simpler answer which uses a trick right from the top of the example(order) code:

R> dd[with(dd, order(-z, b)), ] b x y z 4 Low C 9 2 2 Med D 3 1 1 Hi A 8 1 3 Hi A 9 1 

Edit some 2+ years later: It was just asked how to do this by column index. The answer is to simply pass the desired sorting column(s) to the order() function:

R> dd[ order(-dd[,4], dd[,1]), ] b x y z 4 Low C 9 2 2 Med D 3 1 1 Hi A 8 1 3 Hi A 9 1 R> 

rather than using the name of the column (and with() for easier/more direct access).

dealing with "invalid argument to unary operator" error
Source Link
Richie Cotton
  • 121.7k
  • 47
  • 254
  • 371

You can use the order() function directly without resorting to add-on tools -- see this simpler answer which uses a trick right from the top of the example(order) code:

R> dd[with(dd, order(-z, b)), ] b x y z 4 Low C 9 2 2 Med D 3 1 1 Hi A 8 1 3 Hi A 9 1 

Edit some 2+ years later: It was just asked how to do this by column index. The answer is to simply pass the desired sorting column(s) to the order() function:

R> dd[ orderdd[order(-dd[,4], dd[,1]), ] b x y z 4 Low C 9 2 2 Med D 3 1 1 Hi A 8 1 3 Hi A 9 1 R> 

rather than using the name of the column (and with() for easier/more direct access).


If you use minus with a character column, you get an error:

dd$x <- as.character(dd$x) dd[order(-dd[,2], dd[,1]), ] ## Error in -dd[, 2] : invalid argument to unary operator 

In this case you need to wrap the argument in xtfrm.

dd[order(-xtfrm(dd[,2]), dd[,1]), ] 

You can use the order() function directly without resorting to add-on tools -- see this simpler answer which uses a trick right from the top of the example(order) code:

R> dd[with(dd, order(-z, b)), ] b x y z 4 Low C 9 2 2 Med D 3 1 1 Hi A 8 1 3 Hi A 9 1 

Edit some 2+ years later: It was just asked how to do this by column index. The answer is to simply pass the desired sorting column(s) to the order() function:

R> dd[ order(-dd[,4], dd[,1]), ] b x y z 4 Low C 9 2 2 Med D 3 1 1 Hi A 8 1 3 Hi A 9 1 R> 

rather than using the name of the column (and with() for easier/more direct access).

You can use the order() function directly without resorting to add-on tools -- see this simpler answer which uses a trick right from the top of the example(order) code:

R> dd[with(dd, order(-z, b)), ] b x y z 4 Low C 9 2 2 Med D 3 1 1 Hi A 8 1 3 Hi A 9 1 

Edit some 2+ years later: It was just asked how to do this by column index. The answer is to simply pass the desired sorting column(s) to the order() function:

R> dd[order(-dd[,4], dd[,1]), ] b x y z 4 Low C 9 2 2 Med D 3 1 1 Hi A 8 1 3 Hi A 9 1 R> 

rather than using the name of the column (and with() for easier/more direct access).


If you use minus with a character column, you get an error:

dd$x <- as.character(dd$x) dd[order(-dd[,2], dd[,1]), ] ## Error in -dd[, 2] : invalid argument to unary operator 

In this case you need to wrap the argument in xtfrm.

dd[order(-xtfrm(dd[,2]), dd[,1]), ] 
Correct link to **order()** not sort()
Source Link
Dirk is no longer here
  • 369.9k
  • 60
  • 668
  • 742
Loading
added 398 characters in body
Source Link
Dirk is no longer here
  • 369.9k
  • 60
  • 668
  • 742
Loading
Source Link
Dirk is no longer here
  • 369.9k
  • 60
  • 668
  • 742
Loading