You can achieve the required functionality with dplyr::lag()
function; consider the example below:
What it does is it computes a technical column called duplicate, which is based on the value of current name
column, compared to the previous row and previous by two. TRUE for all three values equal, FALSE for different (note the NA in first row where lag is undefined).
You can then use it easily for eliminating the offending rows, if so desired.
library(dplyr)
animals <- tibble(row = 1:6,
name = c("cat", "dog", "cat", "dog", "dog", "dog"))
animals <- animals %>%
mutate(duplicate = ifelse(name == lag(name, n = 1)
& name == lag(name, n = 2), T, F))
print(animals)
row name duplicate
<int> <chr> <lgl>
1 1 cat NA
2 2 dog FALSE
3 3 cat FALSE
4 4 dog FALSE
5 5 dog FALSE
6 6 dog TRUE