I have multiple missing values in subsequent rows in a data frame. I want to replace the first missing value with the non-missing value in its previous row multiplied by 3 and then fill in the next NA values using the new-filled values in their previous rows multiplied by 3. Here is an example of data and codes:
df <- mtcars
df[c(2,3,4,5),1] <-NA
IND <- is.na(df[,1])
df[IND,1] <- df[dplyr::lead(IND,1L, F),1] * 3
The last line of the above codes does the job but row by row (I should run it 4 times to fill in the 4 missing rows). How can I do it once for all four missing rows?
I want:
df[c(2,3,4,5),1] <- c(63,189,567,1701)
using one loop instead of using the above code four times.