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

Viewed 36
  • 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?
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)]

Related