I made a function to increase a counter whenever the value in a vector changes:
track_change = function(x) {
counter = numeric(length(x))
for (i in seq_along(x)) {
if (i > 1 && x[i] != x[i-1])
counter[i] = counter[i-1] + 1
else if (i > 1)
counter[i] = counter[i-1]
}
counter
}
Example (see how track
changes upon every change of carb
):
mtcars$track = track_change(mtcars$carb)
head(mtcars[, 10:12])
gear carb track
Mazda RX4 4 4 0
Mazda RX4 Wag 4 4 0
Datsun 710 4 1 1
Hornet 4 Drive 3 1 1
Hornet Sportabout 3 2 2
Valiant 3 1 3
Is there a better way to do this in R? (It should also be able to track changes in non-numerical vectors, incl. lists.)