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

1
  • This post is related, though not exactly the same. Commented Apr 7, 2020 at 22:17

2 Answers 2

3

We can use decreasing = TRUE

temp[order(as.character(temp$y), decreasing = TRUE),]
Sign up to request clarification or add additional context in comments.

2 Comments

sorry, I meant, I want to order by ascending x and descending y. like temp[order(x, -as.character(y)), ]
@sunflower In that case, it would give the order of iinput example. Check temp %>% arrange(x, desc(as.character(y))) (from Lennyy's solution)
2

With dplyr:

temp %>% 
  arrange(desc(y))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.