Not sure tibbletime has a lot to help here. This is kind of a custom special case.
For this specific case you can do:
library(dplyr)
library(tibble)
df <- tribble(
~date, ~group, ~score,
"2015-01-01", "A", 10,
"2015-02-01", "A", NA,
"2015-12-01", "A", NA,
"2015-02-01", "A", 5,
"2015-01-01", "B", 10,
"2015-02-01", "B", NA,
"2015-12-01", "B", NA
)
df$date <- as.Date(df$date)
df %>%
group_by(group) %>%
mutate(score_lag = lag(score),
score_imputed = case_when(
!is.na(score) ~ score,
!is.na(score_lag) ~ score_lag,
TRUE ~ NA_real_
))
#> # A tibble: 7 x 5
#> # Groups: group [2]
#> date group score score_lag score_imputed
#> <date> <chr> <dbl> <dbl> <dbl>
#> 1 2015-01-01 A 10. NA 10.
#> 2 2015-02-01 A NA 10. 10.
#> 3 2015-12-01 A NA NA NA
#> 4 2015-02-01 A 5. NA 5.
#> 5 2015-01-01 B 10. NA 10.
#> 6 2015-02-01 B NA 10. 10.
#> 7 2015-12-01 B NA NA NA
If you need this to be more general then there might be more work to do, but this does what you request with this small example.