I have daily meteorological data (temperature and precipitation) from 1955 to 2017 from different locations and I want to summarize each variable into monthly averages but only if the number of NAs in each month is less than 10.
I put four months of temperature data with different amounts of NAs as example (1st month: 1 NA, 2nd month(31days): 30 NA, 3rd month: 0 NA, 4th month: all data as NA):
library(dplyr)
library(lubridate)
exmpldf <- data.frame(DATE = c("1955-06-01", "1955-06-02", "1955-06-03", "1955-06-04", "1955-06-05", "1955-06-06", "1955-06-07", "1955-06-08", "1955-06-09", "1955-06-10",
"1955-06-11", "1955-06-12", "1955-06-13", "1955-06-14", "1955-06-15", "1955-06-16", "1955-06-17", "1955-06-18", "1955-06-19", "1955-06-20",
"1955-06-21", "1955-06-22", "1955-06-23", "1955-06-24", "1955-06-25", "1955-06-26", "1955-06-27", "1955-06-28", "1955-06-29", "1955-06-30",
"1955-07-01", "1955-07-02", "1955-07-03", "1955-07-04", "1955-07-05", "1955-07-06", "1955-07-07", "1955-07-08", "1955-07-09", "1955-07-10",
"1955-07-11", "1955-07-12", "1955-07-13", "1955-07-14", "1955-07-15", "1955-07-16", "1955-07-17", "1955-07-18", "1955-07-19", "1955-07-20",
"1955-07-21", "1955-07-22", "1955-07-23", "1955-07-24", "1955-07-25", "1955-07-26", "1955-07-27", "1955-07-28", "1955-07-29", "1955-07-30",
"1955-07-31", "1955-08-01", "1955-08-02", "1955-08-03", "1955-08-04", "1955-08-05", "1955-08-06", "1955-08-07", "1955-08-08", "1955-08-09",
"1955-08-10", "1955-08-11", "1955-08-12", "1955-08-13", "1955-08-14", "1955-08-15", "1955-08-16", "1955-08-17", "1955-08-18", "1955-08-19",
"1955-08-20", "1955-08-21", "1955-08-22", "1955-08-23", "1955-08-24", "1955-08-25", "1955-08-26", "1955-08-27", "1955-08-28", "1955-08-29",
"1955-08-30", "1955-08-31", "1955-09-01", "1955-09-02", "1955-09-03", "1955-09-04", "1955-09-05", "1955-09-06", "1955-09-07", "1955-09-08",
"1955-09-09", "1955-09-10", "1955-09-11", "1955-09-12", "1955-09-13", "1955-09-14", "1955-09-15", "1955-09-16", "1955-09-17", "1955-09-18",
"1955-09-19", "1955-09-20", "1955-09-21", "1955-09-22", "1955-09-23", "1955-09-24", "1955-09-25", "1955-09-26", "1955-09-27", "1955-09-28",
"1955-09-29", "1955-09-30"),
TMAX = c(NA, 20, 27, 17, 26.5, 27, 17, 26.5, 20, 23, 23, 21.5, 24, 26.5, 27, 27, 26.5, 24.5, 23, 22.5, 24, 23, 21.5, 25, 26.5, 23,
24, 23.5, 23, 23, 23, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, 24, 22, 21, 17, 17, 17, 21.5, 22, 22, 22.5, 22.5, 16.5, 20.5, 17.5, 23, 17, 21, 21.5, 21, 21, 20, 22, 22, 22, 21.5, 21.5, 21.5, 22.5, 20,
21, 20, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA))
For the monthly aggregation I used mutate to create a column "MONTH" and a column "YEAR"
exmpldf <- exmpldf %>%
mutate(month(DATE), year(DATE))
names(exmpldf) <- c("DATE", "TMAX", "MONTH", "YEAR")
To create the monthly average I used summarize:
exmpldfmeanMonth <- exmpldf %>%
group_by(MONTH, YEAR) %>%
summarise(TMAX = mean(TMAX))
The problem is, in my time series (1955-2017) there are many months that have at least 1 daily data as NA and other months have all or almost all daily data as NA, in any case, the monthly mean is NA:
> exmpldfmeanMonth
# A tibble: 4 x 3
# Groups: MONTH [4]
MONTH YEAR TMAX
<dbl> <dbl> <dbl>
1 6 1955 NA (1 day is NA)
2 7 1955 NA (all days but 1, are NA)
3 8 1955 20.7 (no NAs)
4 9 1955 NA (all days are NA)
You could add na.rm = T but then it calculates the mean even if there is only one data per month:
exmpldfmeanMonth <- exmpldf %>%
group_by(MONTH, YEAR) %>%
summarise(TMAX = mean(TMAX, na.rm = T))
> exmpldfmeanMonth
# A tibble: 4 x 3
# Groups: MONTH [4]
MONTH YEAR TMAX
<dbl> <dbl> <dbl>
1 6 1955 23.7 (1 day is NA)
2 7 1955 23 (all days but 1, are NA)
3 8 1955 20.7 (no NAs)
4 9 1955 NaN (all days are NA)
So I would like to generate a conditional that calculates the monthly average only if there are 10 or less NAs per month, otherwise it should be considered as NA:
> exmpldfmeanMonth
# A tibble: 4 x 3
# Groups: MONTH [4]
MONTH YEAR TMAX
<dbl> <dbl> <dbl>
1 6 1955 23.7 (1 day is NA)
2 7 1955 NAN (all days but 1, are NA)
3 8 1955 20.7 (no NAs)
4 9 1955 NaN (all days are NA)
Could you please guide me on how to solve this? Thank you very much in advance!