From this hourly data, I need to find the value of NA, calculating the average Temp of the previous day and the next day at the same time (1200).
The data format is as follows: Where n is the day number of the year. The time is raw (0 = 00:00, 100 = 1:00, etc).
Date | n | hour | temperature |
---|---|---|---|
09/01/2016 | 9 | 600 | 8.4 |
09/01/2016 | 9 | 700 | 8.2 |
09/01/2016 | 9 | 800 | 7.8 |
09/01/2016 | 9 | 900 | 8.7 |
09/01/2016 | 9 | 1000 | 13.6 |
09/01/2016 | 9 | 1100 | 16.6 |
09/01/2016 | 9 | 1200 | 19.7 |
01/10/2016 | 10 | 1200 | NA |
01/11/2016 | eleven | 1200 | 18.9 |
01/11/2016 | eleven | 1300 | 20.9 |
01/11/2016 | eleven | 1400 | 23.3 |
01/11/2016 | eleven | 1500 | 24.3 |
There are many NAs in different periods, so I am trying to obtain a code that allows me to calculate the averages of the day before and after the hour where there is an NA.
Thank you
See if this code works for you, I'm commenting on it.
Your data:
The first thing is to convert
Date
to R date format and select the temperatures withNA
:Now we select the days before and after the days you have with
NA
:Now we are going to join with the dataset of
NAs
per hour, and for each hour withNA
we are left with those records of the day before and after. Finally, we do the average:Finally, we paste the calculated average to the initial dataset. In this case in a new column
Temp_end
to see the complete process:Created on 2022-10-05 with reprex v2.0.2