I am looking to extract or filter rows that contain a specific value together with a number of rows that precedes the row with the specific value.
In the example below I wish to keep the row where Y == "HIT"
and I also wish to keep the four rows that precedes it. To be specific, I wish to extract rows 6 to 10.
I have written some code that does the job, but I am convinced there must be a better solution.
Can you help me find it? Base R
or dplyr
solutions are preferred.
Thanks in advance.
library(tidyverse)
# Data
df <- tibble(X = 1:11,
Y = c(rep("MISS", 9), "HIT", "MISS"))
df
#> # A tibble: 11 x 2
#> X Y
#> <int> <chr>
#> 1 1 MISS
#> 2 2 MISS
#> 3 3 MISS
#> 4 4 MISS
#> 5 5 MISS
#> 6 6 MISS
#> 7 7 MISS
#> 8 8 MISS
#> 9 9 MISS
#> 10 10 HIT
#> 11 11 MISS
# I want to keep the the row with the value "HIT" in column Y and the 4 rows that preecedes it.
#My code monster that kind of works
df_monster <- df %>%
mutate(
Z = if_else(Y == "HIT", 5, 0),
Z = case_when(
Z > 0 ~ Z,
lead(Z) > 0 ~ lead(Z)-1,
TRUE ~ 0)
) %>%
mutate(
Z = case_when(
Z > 0 ~ Z,
lead(Z) > 0 ~ lead(Z)-1,
TRUE ~ 0)
) %>%
mutate(
Z = case_when(
Z > 0 ~ Z,
lead(Z) > 0 ~ lead(Z)-1,
TRUE ~ 0)
) %>%
mutate(
Z = case_when(
Z > 0 ~ Z,
lead(Z) > 0 ~ lead(Z)-1,
TRUE ~ 0)
)
#Desired result
df_monster %>%
filter(Z > 0)
#> # A tibble: 5 x 3
#> X Y Z
#> <int> <chr> <dbl>
#> 1 6 MISS 1
#> 2 7 MISS 2
#> 3 8 MISS 3
#> 4 9 MISS 4
#> 5 10 HIT 5
Created on 2021-08-17 by the reprex package (v2.0.0)