You could do something like this (inspired by the comments and decisions of Frank and eddi):
df$value2 <- ave(df$value, df$GROUP, cumsum(!is.na(df$value)), FUN = function(x) x[1] + 0.065 * (1:length(x) - 1))
Or my original ave :
df$value2 <- ave(df$value, df$GROUP, FUN = function(x) {nas_to_replace <- is.na(x) & seq_along(x) > tail(which(!is.na(x)),1) replace(x, nas_to_replace, tail(x[!is.na(x)],1) + 0.065*(1:sum(nas_to_replace)))} )
This function is intended to replace only NA that appear after the last non-NA. Therefore, if you have a vector of type c(NA, 1, 2, NA, NA) , it will replace only the last two elements.
head(df) # Date GROUP value value2 #1 2018-01-31 10180 3.464 3.464 #2 2018-02-28 10180 3.413 3.413 #3 2018-03-31 10180 3.418 3.418 #4 2018-04-30 10180 NA 3.483 #5 2018-05-31 10180 NA 3.548 #6 2018-06-30 10180 NA 3.613
source share