Hi
I have data like this which has repeating numbers
c(1,1,1,4,6,1,1,1, 7,7,1,1,1,...)
I would like to have this transformed to something like:
c(1,2,3,4,6,1,2,3,7,8, 1,2,3,...)
All the repeating numbers should be replaced by numbers that go up 1 at a time. Those that do not repeat remain the same.
Any ideas?
Cheers
Renger
Good old for()
loop to the rescue.
x <- c(1, 1, 1, 4, 6, 1, 1, 1, 7, 7, 1, 1, 1)
output_vec <- vector("numeric", length = length(x))
for (i in seq_along(x)) {
if (i == 1) {
output_vec[[i]] <- x[[i]]
} else if (x[[i]] - x[[i - 1]] == 0) {
output_vec[[i]] <- output_vec[[i - 1]] + 1
} else {
output_vec[[i]] <- x[[i]]
}
}
print(output_vec)
#> [1] 1 2 3 4 6 1 2 3 7 8 1 2 3
Created on 2020-10-19 by the reprex package (v0.3.0)
1 Like
system
Closed
3
This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.
If you have a query related to it or one of the replies, start a new topic and refer back with a link.