Hi, and welcome!
Please see the FAQ: What's a reproducible example (`reprex`) and how do I do one? Using a reprex, complete with representative data will attract quicker and more answers. This question is not complex enough to require one, though.
suppressPackageStartupMessages(library(dplyr))
dat <- structure(list(
recid =
c(1, 2, 3),
obs =
c(0, 4, 8),
subj =
c(1, 1, 1),
score =
c(20, 40, 60)
),
class =
c("spec_tbl_df", "tbl_df", "tbl", "data.frame"),
row.names =
c(NA, -3L), spec = structure(list(cols = list(recid = structure(list(),
class =
c("collector_double", "collector")
), obs = structure(list(),
class =
c("collector_double", "collector")
), subj = structure(list(),
class =
c("collector_double", "collector")
), score = structure(list(),
class =
c("collector_double", "collector")
)), default = structure(list(),
class =
c("collector_guess", "collector")
), skip = 1),
class = "col_spec"
)
)
dat
#> # A tibble: 3 x 4
#> recid obs subj score
#> <dbl> <dbl> <dbl> <dbl>
#> 1 1 0 1 20
#> 2 2 4 1 40
#> 3 3 8 1 60
dat %>%
dplyr::mutate(change = ifelse(score > lag(score, 2), "success", "failure")) %>%
filter(obs == 8)
#> # A tibble: 1 x 5
#> recid obs subj score change
#> <dbl> <dbl> <dbl> <dbl> <chr>
#> 1 3 8 1 60 success
Created on 2020-03-28 by the reprex package (v0.3.0)