1
temp <- data.frame(x=c(1,2,3), y=c("ba", "ab", "cc"))
temp
  x y
1 1 ba
2 2 ab
3 3 cc

and I want to order by ascending x and descending y, but it doesn't work even I convert y from factor to string.

tt <- temp[order(-as.character(y)), ]  

Error in -as.character(y) : invalid argument to unary operator

Rui Barradas
  • 70,273
  • 8
  • 34
  • 66
sunflower
  • 53
  • 5

2 Answers2

3

We can use decreasing = TRUE

temp[order(as.character(temp$y), decreasing = TRUE),]
akrun
  • 874,273
  • 37
  • 540
  • 662
  • sorry, I meant, I want to order by ascending x and descending y. like temp[order(x, -as.character(y)), ] – sunflower Apr 08 '20 at 13:43
  • @sunflower In that case, it would give the order of iinput example. Check `temp %>% arrange(x, desc(as.character(y)))` (from Lennyy's solution) – akrun Apr 08 '20 at 17:19
2

With dplyr:

temp %>% 
  arrange(desc(y))
Lennyy
  • 5,932
  • 2
  • 10
  • 23