I have the following data…
have_df <- tibble(
year = c(2015, 2016, 2017, 2018, 2019, 2015, 2016, 2017, 2018, 2019),
da_assist = c(0, 0, 0, 1, 1, 0, 0, 0, 0, 0),
priority = c(NA, NA, NA, "Priority4", "Priority4", NA, NA, NA, NA, NA),
nces_dist = c(065441, 065441, 065441, 065441, 065441,074911, 074911, 074911, 074911, 074911))
I would like to use dplyr to say when ‘priority’ in 2018 = ‘Priority4’ then turn the NAs for ‘priority’ in 2015, 2016, and 2017 to 'Priority4'. I want to change the values in the priority variable only for those three years for the specific ids (nces_dist) where ‘priority’ in 2018 = ‘Priority4’, so the data would look like this:
need_df <- tibble(
year = c(2015, 2016, 2017, 2018, 2019, 2015, 2016, 2017, 2018, 2019),
da_assist = c(0, 0, 0, 1, 1, 0, 0, 0, 0, 0),
priority = c("Priority4", "Priority4", "Priority4","Priority4", "Priority4", NA, NA, NA, NA, NA),
nces_dist = c(065441, 065441, 065441, 065441, 065441,074911, 074911, 074911, 074911, 074911)
I’ve tried searching through a dozen mutate posts but can’t find a way to mutate a subset of a variable using a subset from another variable. Thanks.