Noir15
Noir15

Reputation: 11

How do I sort a vector in ascending order in intervals in R language?

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

Answers (1)

r.user.05apr
r.user.05apr

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

Related Questions