I would like to add all missing dates between min and max date in a data.frame
and linear interpolate all missing values, like
df <- data.frame(date = as.Date(c("2015-10-05","2015-10-08","2015-10-09",
"2015-10-12","2015-10-14")),
value = c(8,3,9,NA,5))
date value
2015-10-05 8
2015-10-08 3
2015-10-09 9
2015-10-12 NA
2015-10-14 5
date value approx
2015-10-05 8 8
2015-10-06 NA 6.33
2015-10-07 NA 4.67
2015-10-08 3 3
2015-10-09 9 9
2015-10-10 NA 8.20
2015-10-11 NA 7.40
2015-10-12 NA 6.60
2015-10-13 NA 5.80
2015-10-14 5 5
Is there a clear solution with dplyr
and approx
?
(I do not like my 10 line for
loop code.)
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…