[Solved] Complete missing values in time series using previous day data – using R

If I understand correctly, the trick is that you want to fill downward except for the bottommost NAs. And the problem with tidyr‘s fill is that it goes all the way down. This isn’t a fully-tidyverse solution, but for this data: library(dplyr) library(tidyr) data <- tribble( ~Date, ~time_series_1, ~time_series_2, as.Date(“2019-01-01”), NA, 10, as.Date(“2019-02-01”), 5, NA, … Read more

[Solved] How do I replace all NA with mean in R? [duplicate]

We can use na.aggregate from zoo. Loop through the columns of dataset (assuming all the columns are numeric ), apply the na.aggregate to replace the NA with mean values (by default) and assign it back to the dataset. library(zoo) df[] <- lapply(df, na.aggregate) By default, the FUN argument of na.aggregate is mean: Default S3 method: … Read more