Resolved: How can I replace vector values with ascending numbers using the same intervals 0 By Isaac Tonny on 17/06/2022 Issue Share Facebook Twitter LinkedIn Question: I am trying to replace values of a vector with different intervals, such that the intervals are numbered from 1 to last interval. For example, x <- c(5, 5, 5, 7, 7, 7, 9, 11, 11, 11, 15, 15, 15, 17, 17, 17, 17) [/code] I want to be like this: [code]y <- c(1, 1, 1, 2, 2, 2, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6, 6) [/code] Is there a way to this automatically? Thanks for any help. Follow-up question: What if you want to keep the gabs in your new vector? For example, if we have: [code]x <- c(5, 5, 7, 7, 8, 8, 9) [/code] I want it to be: [code]y <- c(1, 1, 3, 3, 4, 4, 5) [/code] Answer: You can do [code]match(x, unique(x)) #> [1] 1 1 1 2 2 2 3 4 4 4 5 5 5 6 6 6 6 Or as.numeric(factor(x)) #> [1] 1 1 1 2 2 2 3 4 4 4 5 5 5 6 6 6 6 Or 1 + c(0, cumsum(sign(diff(x)))) #> [1] 1 1 1 2 2 2 3 4 4 4 5 5 5 6 6 6 6 If you have better answer, please add a comment about this, thank you! r