reproducible data which YOU should provide:
df <- mtcars
df[c(1,5,8),1] <-NA
code:
IND <- is.na(df[,1])
df[IND,1] <- df[dplyr::lag(IND,1L, F),1] * 3
- since you use lag I use lag. You are saying “previous”. So maybe you want to use
lead
. - What happens if the first value in lead case or last value in lag case is missing. (this remains a mystery)
1
solved Loop over NA values in subsequent rows in R [closed]