Count of number of elements between distinct elements in vector

Or

ave(seq.int(x), x, FUN = function(x) c(NA, diff(x)))
#  [1] NA NA  2 NA  2  4  1  4  1  3  1  7  1  1  6  1  1  1  8  6

We calculate the first difference of the indices for each group of x.


A data.table option thanks to @Henrik

library(data.table)
dt = data.table(x)
dt[ , d := .I - shift(.I), x]
dt

Here's a function that would work

compute_lag_counts <- function(x) {
  seqs <- split(seq_along(x), x)
  unsplit(Map(function(i) c(NA, diff(i)), seqs), x)
}

compute_lag_counts (x)
# [1] NA NA  2 NA  2  4  1  4  1  3  1  7  1  1  6  1  1  1  8  6

Basically you use split() to separate the indexes where values appear by each unique value in your vector. Then we use the different between the index where they appear to calculate the distance to the previous value. Then we use unstack to put those values back in the original order.


An option with dplyr by taking the difference of adjacent sequence elements after grouping by the original vector

library(dplyr)
tibble(v1) %>% 
   mutate(ind = row_number()) %>%
   group_by(v1) %>% 
   mutate(new = ind - lag(ind)) %>%
   pull(new)
#[1] NA NA  2 NA  2  4  1  4  1  3  1  7  1  1  6  1  1  1  8  6

data

v1 <- c("A", "C", "A", "B", "A", "C", "C", "B", "B", "C", "C", "A", 
"A", "A", "B", "B", "B", "B", "C", "A")