I have data:
Date | Price |
---|---|
"2021-01-01" | 1 |
"2021-01-02" | NA |
"2021-01-03" | NA |
"2021-01-04" | NA |
"2021-01-05" | NA |
"2021-01-06" | 6 |
"2021-01-07" | NA |
"2021-01-08" | NA |
"2021-01-09" | 3 |
And I would like to replace missing values with means, so that the end result would look like this:
Date | Price |
---|---|
"2021-01-01" | 1 |
"2021-01-02" | 2 |
"2021-01-03" | 3 |
"2021-01-04" | 4 |
"2021-01-05" | 5 |
"2021-01-06" | 6 |
"2021-01-07" | 5 |
"2021-01-08" | 4 |
"2021-01-09" | 3 |
You can use zoo::na.approx
:
library(zoo)
na.approx(dat$Price)
# [1] 1 2 3 4 5 6 5 4 3