Thanks for the help!
I want the vector of:
s = c(2, 2, 2, 4, 4, 3, 5, 1)
to be
s = c(1, 1, 1, 2, 2, 3, 4, 5)
.
I am not sorting!
Use rle
and inverse.rle
from the base R.
s <- c(2, 2, 2, 4, 4, 3, 5, 1)
rl <- rle(s)
rl$values <- sort(unique(s))
s <- inverse.rle(rl)
s
# [1] 1 1 1 2 2 3 4 5