Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why Order() in R don't return the correct order?

Tags:

function

r

  • I run these simple code in R and I don't know why it doesn't return the correct order.

order(c(4,1,4,3,2))

order(c("R","C","R","I","J"))

  • This is the results:

[1] 2 5 4 1 3

[1] 2 4 5 1 3

  • What is the problem?
like image 893
conquyhung Avatar asked Jan 18 '26 12:01

conquyhung


1 Answers

The function order() returns the id (location) of the sorted elements. You could use:

v1 <- c(4,1,4,3,2)
v2 <- c("R","C","R","I","J")

sort(v1)
sort(v2)

#or

v1 <- v1[order(v1)]
v2 <- v2[order(v2)]

like image 63
Pedro Alencar Avatar answered Jan 20 '26 03:01

Pedro Alencar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!