Home > Mobile >  order vector always ascending without trunc or duplicates
order vector always ascending without trunc or duplicates

Time:08-17

How can I order this vector:

x <- c(4,4,5,5)
[1] 4 4 5 5

to

x <- c(4,5,4,5)
[1] 4 5 4 5

I am not sure. I have tried TRUE, FALSE.

CodePudding user response:

We may use rowid

library(data.table)
x[order(rowid(x))]
[1] 4 5 4 5

CodePudding user response:

With base R we can use ave

> x[order(ave(x, x, FUN = seq_along))]
[1] 4 5 4 5
  • Related