'Data interpolation by conditions
I am trying to fill data gaps (replace Nas) based on different conditions with R in an hourly data frame with many years of ozone data. The conditions I want are for gaps smaller or equal to 3 hours: Fill variable by linear interpolation between the last known value and the next known value. Gaps greater than 3 hours but smaller than 24 hours: Fill variable by linear interpolation between the values of the same hour of the next day. Gaps equal to or greater than 24 hours: Insertion of values of the same Julian day from the previous year. Until now, I have been able to interpolate, but not with conditions. Any help is welcome!!!
My data looks like this:
date<-c("3/6/2018 0:00", "3/6/2018 1:00", "3/6/2018 2:00", "3/6/2018 3:00", "3/6/2018 4:00", "3/6/2018 5:00", "3/6/2018 6:00", "3/6/2018 7:00", "3/6/2018 8:00", "3/6/2018 9:00", "3/6/2018 10:00", "3/6/2018 11:00", "3/6/2018 12:00", "3/6/2018 13:00", "3/6/2018 14:00", "3/6/2018 15:00", "3/6/2018 16:00", "3/6/2018 17:00", "3/6/2018 18:00", "3/6/2018 19:00", "3/6/2018 20:00", "3/6/2018 21:00", "3/6/2018 22:00", "3/6/2018 23:00", "3/7/2018 0:00", "3/7/2018 1:00", "3/7/2018 2:00", "3/7/2018 3:00", "3/7/2018 4:00", "3/7/2018 5:00", "3/7/2018 6:00", "3/7/2018 7:00", "3/7/2018 8:00", "3/7/2018 9:00", "3/7/2018 10:00", "3/7/2018 11:00", "3/7/2018 12:00", "3/7/2018 13:00", "3/7/2018 14:00", "3/7/2018 15:00", "3/7/2018 16:00", "3/7/2018 17:00", "3/7/2018 18:00", "3/7/2018 19:00", "3/7/2018 20:00", "3/7/2018 21:00", "3/7/2018 22:00", "3/7/2018 23:00")
julianDay<-c("65", "65", "65", "65", "65", "65", "65", "65", "65", "65", "65", "65", "65", "65", "65", "65", "65", "65", "65", "65", "65", "65", "65", "65", "66", "66", "66", "66", "66", "66", "66", "66", "66", "66", "66", "66", "66", "66", "66", "66", "66", "66", "66", "66", "66", "66", "66", "66")
Ozone<-c("2.58", "2.69", "2.65", "2.86", "2.66", "3.19", "2.87", "3.16", "2.75", "2.18", "NA", "NA", "1.56", "2.19", "1.44", "NA", "NA", "NA", "NA", "NA", "1.09", "0.46", "0.77", "0.87", "0.79", "0.76", "0.80", "1.34", "1.89", "1.88", "1.85", "1.66", "1.44", "1.31", "1.06", "1.14", "1.69","2.02","2.66", "2.18", "2.70", "3.00", "3.28", "3.80", "3.78", "3.83", "2.68", "3.14")
df<-data.frame(date, julianDay, Ozone)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
