daily_mean: Mean of daily weather variable values between two dates

Description Usage Arguments Details Value See Also Examples

View source: R/daily_mean.R

Description

Calculates the average of daily values of the variable between two dates from a tibble or data frame of daily weather data. Alternatively, a number of days before or after a specific date may be specified. Typically this would be used for temperature, rainfall or solar radiation.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
daily_mean(
  data,
  var = NULL,
  datevar = NULL,
  ndays = 5,
  na.rm = FALSE,
  startdate = NULL,
  enddate = NULL,
  monitor = FALSE,
  warn.consecutive = TRUE,
  ...
)

Arguments

data

Tibble or dataframe of daily weather data

var

Variable(s) to be extracted (Default: radn). Several columns may be specified using column names c(variable1, variable2, ...)

datevar

Date variable specifying day (Default: date_met)

ndays

Number of days after/before the start or end date, respectively. Ignored of both the start and end dates are specified (Default: 5)

na.rm

Used for calculations (Default: FALSE)

startdate

Start date of data to be extracted

enddate

Final date of data to be extracted

monitor

For debugging. Prints data and dates. (Default: FALSE)

warn.consecutive

A logical indicating whether to check that dates are consecutive, that none are missing and provide a warning if not (Default:TRUE)

...

options to be passed to mean calculation

Details

The mean is returned but if there are any missing values, then the mean is set to NA since the default na.rm is TRUE. Note that if there are any missing dates, then a warning is issued but the mean of non-missing values is returned.

If any values are missing, while the calculated mean may prove useful, it will not include all the data and so may lead to biased estimates. Hence, in these cases, the appropriateness of the estimate will depend on the exact circumstances of the missing data and so this decision is left to the user.

Value

Numeric variable containing the daily mean of the weather variable var during the specified period.

See Also

mean, cumulative, growing_degree_days, stress_days_over, and weather_extract

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
##  Selected calculations
library(tidyverse)
daily_mean(boonah, enddate = crop$flower_date[4], ndays = 3,
                    monitor = TRUE)
daily_mean(boonah, enddate = crop$harvest_date[4], ndays = 3,
                    monitor = TRUE)
daily_mean(boonah, startdate = crop$flower_date[4],
                    enddate = crop$harvest_date[4], monitor = TRUE)

## Add selected daily means of weather variables in 'boonah' to 'crop'
## tibble using 'map2_dbl' from the 'purrr' package
## Note: using equivalent 'furrr' functions can speed up calculations 
crop2 <- crop %>%
  mutate(mean_maxtemp_post_sow_7d =
          map_dbl(sowing_date, function(x)
            daily_mean(boonah, var = maxt, startdate = x, ndays = 7)),
          mean_rad_flower_harvest =
            map2_dbl(flower_date, harvest_date, function(x, y)
              daily_mean(boonah, var = radn, startdate = x, enddate = y)))
crop2

cropgrowdays documentation built on Dec. 11, 2021, 9:23 a.m.