Reputation: 11
Suppose that I have a vector named x = c(3,2,1,2,1,3,1,3,2). I need help in an algorithm that will help me sort the values in x in ascending order like this (1,2,3,1,2,3,1,2,3).
Upvotes: 1
Views: 83
Reputation: 5456
You can do:
x <- c(3,2,1,2,1,3,1,3,2)
as.vector(apply(matrix(x, nrow = 3), 2, sort))
Upvotes: 2