Artie Ladie
Artie Ladie

Reputation: 521

dplyr Arrange does not work

Reference question, Order color based on Hue, Saturation, Value in R

I have the following dataframe that I wish to sort based on h, then s, and finally v

> library(dplyr)
> tHSVcol
             h         s         v
[1,] 0.6229508 0.6421053 0.3725490
[2,] 0.2767296 0.5145631 0.8078431
[3,] 0.1323283 0.7928287 0.9843137
[4,] 0.9790476 0.9510870 0.7215686
[5,] 0.9093567 0.5480769 0.4078431

When I execute tHSVcol %>% arrange(desc(h)), I get

Error in UseMethod("arrange_") : no applicable method for 'arrange_' applied to an object of class "c('matrix', 'double', 'numeric')"

What am I doing wrong?

Upvotes: 1

Views: 4281

Answers (1)

patL
patL

Reputation: 2299

Supposing your matrix is called m, you can do:

m[order(m[,"h"], decreasing = T), ]

#             h         s         v
#[1,] 0.9790476 0.9510870 0.7215686
#[2,] 0.9093567 0.5480769 0.4078431
#[3,] 0.6229508 0.6421053 0.3725490
#[4,] 0.2767296 0.5145631 0.8078431
#[5,] 0.1323283 0.7928287 0.9843137

Upvotes: 1

Related Questions