'R find pattern in vector within participant id

I have looked at this question but it seems to me it addresses a different issue: Find a numeric pattern R

I have a large data set with multiple observations per id. Observation length (in m example below t) can vary across ids. I want to find patterns as defined by a subject making the same type of decision (below as type) at least three times in a row. My data looks like so:

id <- rep(1:3, each = 5)
t <- rep(1:5, 3)
type <- c("familiar", "familiar", "new", "completely new", "new", "new", "new", "new","new","new","new", "familiar", "completely new", "familiar", "new")
n <- data.frame( id, t, type )
n

How can I find patterns and indicate in a new column how many times they have made the decision to choose a certain type at least three times in a row?

(edit:) My desired output would be a value indicating the type a certain type was chosen at least three times in a row, e.g. something along the lines of "familiar_3+" or "new_3+".

Any help is much appreciated.



Solution 1:[1]

If the number of consecutive observations is fixed, you can do this using lag (or lead). Something like the following:

library(dplyr)

df %>%
  group_by(id) %>%
  mutate(prev1_type = lag(type, 1, order_by = t),
         prev2_type = lag(type, 2, order_by = t)) %>%
  mutate(consec_x3 = ifelse(type == prev1_type & type == prev2_type, "yes", "no"))

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1