51
This can be done in an efficient way by using indexes of where the values change:
x <- c(1,0,0,0,1,2,1,0,0,1,1)
Find where the values change:
diffs <- x[-1L] != x[-length(x)]
Get the indexes, and then get the difference in subsequent indexes:
idx <- c(which(diffs), length(x))
diff(c(0, idx))