# identify series of values preceded and followed by a certain value

I have a sequence of integers. What I would like to do is identify all sequences of 3's that is preceded AND followed by a 5. For example:

``````c(5,3,3,5,5,4,3,3,5)
``````

The desired output would be:

``````c(F,T,T,F,F,F,F,F,F)
``````

Explanation: The first sequence of 3's is preceded and followed by a 5. Hence `True` . The second sequence is preceded by a 4, hence `False`.

On

Couldn't come up with a smarter solution so here is a `for` loop

``````x <- c(5,3,3,5,5,4,3,3,5) #Initial vector
current_inds <- numeric() #Variable to hold indices which need to be changed
saw_3 <- FALSE  #If 3 was seen before
output <- rep(FALSE, length(x))  #output vector
num_to_check <- 5   #Value to compare
last_val <- 0 #Last non-3 value

for (i in seq_along(x)) {
#If the current value is not equal to 3
if (x[i] != 3 ) {
#Check if we previously saw 3 and if previous non_3 value was 5
# and the next value is 5
if(saw_3 & x[i + 1] == num_to_check & last_val == num_to_check) {
#Change the group of 3 indices to TRUE
output[current_inds] <- TRUE
#Make the saw_3 flag as FALSE
saw_3 <- FALSE
}
#Update the last seen non_3 value to current value
last_val = x[i]
}
else {
#If it is a 3 then append the indices in current_inds
current_inds <- c(current_inds, i)
#Make saw_3 flag TRUE
saw_3 = TRUE
}
}

output
#[1] FALSE  TRUE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE
``````
On

I have a very lengthy & ugly solution, but it works :p I hope someone can find a cleaner one :) I first create a matrix that contains 1 column which is every number in a non-repeated way (not unique, but without consecutives), and then 1 column with the number of times this number is repeated. Then I apply a logical function to see if a 3 is surrounded by 5s and in a final step, I unravel the vector back to its original length using the rep() function...

``````x <- c(5,3,3,5,5,4,3,3,5)

x_reduced <- x[x!=c(x[-1], FALSE)]
x_mat <- matrix(0, ncol = 3, nrow = length(x_reduced))
x_mat[ , 1] <- x_reduced

ctr = 1
x_ctr = 1
while (ctr < length(x)) {
x_mat[x_ctr ,1] = x[ctr]
x_mat[x_ctr, 2] = x_mat[x_ctr, 2] + 1
if(x[ctr+1] == x[ctr]){
ctr = ctr + 1
} else {
x_ctr = x_ctr + 1
ctr = ctr + 1
}
}
x_mat[nrow(x_mat), 1] <- x[length(x)]
x_mat[nrow(x_mat), 2] <- x_mat[nrow(x_mat), 2] + 1

check_element <- function(pos) {
if(pos == 1 | pos == nrow(x_mat)) return(FALSE)
if(x_mat[pos+1, 1] == 5 & x_mat[pos-1, 1] == 5){
return(TRUE)
} else {
return(FALSE)
}
}

x_mat[,3] <- sapply(1:nrow(x_mat), check_element)
rep(x_mat[,3], x_mat[,2])

``````
On

There's room for optimization, but it's certainly possible with `dplyr` and `rle()`.

``````> df_result
# A tibble: 9 x 1
result
<lgl>
1 FALSE
2 TRUE
3 TRUE
4 FALSE
5 FALSE
6 FALSE
7 FALSE
8 FALSE
9 FALSE
``````

### Code

``````df_result <- df %>%
group_by(seq = {seq = rle(value); rep(seq_along(seq\$lengths), seq\$lengths)}) %>%
ungroup() %>%
mutate(last_3 = case_when(lag(seq) != seq ~ as.numeric(lag(value) == 5),
TRUE ~ NA_real_),
``````library(dplyr)