Sometimes records (e.g. measurements from passive samplers or miniDOAS) provide values representative irregular time intervals such as starting from odd start times to odd end times. For example 09:58 to 10:08 or 20 Feb to 06 March. This function interpolates the irregular data on a standard interval. It handles down- and upsampling correct.
Usage
wmean(
data,
...,
starttime = "starttime",
endtime = "endtime",
value = "value",
interval = "h1"
)
Arguments
- data
data.frame for averaging; df has to be in long format and contain a start- and end-time column of class POSIXct (arbitrarily named)
- ...
columns containing values for grouping (passed to
dplyr::group_by()
) when calculating weighted means (e.g. for different measurement parameters). Columns not explicitly passed are dropped- starttime
name of starttime column as symbol or string
- endtime
name of endtime column as symbol or string
- value
name of column containing values to be averaged as symbol or string
- interval
specifying the output interval for averaging as string
Value
tibble with the starttime, endtime, value and grouping columns and additional the column "n" containing the sum of weighted intervals within the averaged time interval (data availability in interval, 1 = 100\
Caution
Removes NA values to correctly calculate data availability for each interval
output data isn't padded
Examples
fn <- system.file("extdata", "Zch_Stampfenbachstrasse_h1_2013_Jan.csv", package = "rOstluft.data")
ps_fn <- system.file("extdata", "NO2_PS.rds", package = "rOstluft.data")
data <- read_airmo_csv(fn, time_shift = lubridate::period(25, "minutes"))
data_ps <- readRDS(ps_fn) %>% pluck_site("Zch_Stampfenbachstrasse")
df <- pluck_parameter(data, "CO") %>%
pluck_unit("ppm") %>%
dplyr::mutate(endtime = .data$starttime + lubridate::hours(1)) %>%
dplyr::select(-interval)
wmean(df, site, parameter, unit, interval = "h1")
#> # A tibble: 744 × 7
#> starttime endtime site parameter unit value n
#> <dttm> <dttm> <fct> <fct> <fct> <dbl> <dbl>
#> 1 2013-01-01 00:00:00 2013-01-01 01:00:00 Zch_Stam… CO ppm 0.692 0.583
#> 2 2013-01-01 01:00:00 2013-01-01 02:00:00 Zch_Stam… CO ppm 0.705 1
#> 3 2013-01-01 02:00:00 2013-01-01 03:00:00 Zch_Stam… CO ppm 0.736 1
#> 4 2013-01-01 03:00:00 2013-01-01 04:00:00 Zch_Stam… CO ppm 0.691 1
#> 5 2013-01-01 04:00:00 2013-01-01 05:00:00 Zch_Stam… CO ppm 0.654 1
#> 6 2013-01-01 05:00:00 2013-01-01 06:00:00 Zch_Stam… CO ppm 0.558 1
#> 7 2013-01-01 06:00:00 2013-01-01 07:00:00 Zch_Stam… CO ppm 0.458 1
#> 8 2013-01-01 07:00:00 2013-01-01 08:00:00 Zch_Stam… CO ppm 0.413 1
#> 9 2013-01-01 08:00:00 2013-01-01 09:00:00 Zch_Stam… CO ppm 0.332 1
#> 10 2013-01-01 09:00:00 2013-01-01 10:00:00 Zch_Stam… CO ppm 0.262 1
#> # ℹ 734 more rows
wmean(df, site, parameter, unit, interval = "m1")
#> # A tibble: 2 × 7
#> starttime endtime site parameter unit value n
#> <dttm> <dttm> <fct> <fct> <fct> <dbl> <dbl>
#> 1 2013-01-01 00:00:00 2013-02-01 00:00:00 Zch_Sta… CO ppm 0.375 9.97e-1
#> 2 2013-02-01 00:00:00 2013-03-01 00:00:00 Zch_Sta… CO ppm 0.161 6.20e-4
wmean(df, site, parameter, unit, interval = "y1")
#> # A tibble: 1 × 7
#> starttime endtime site parameter unit value n
#> <dttm> <dttm> <fct> <fct> <fct> <dbl> <dbl>
#> 1 2013-01-01 00:00:00 2014-01-01 00:00:00 Zch_Stam… CO ppm 0.375 0.0847
wmean(data_ps, site, parameter, unit, interval = "d1")
#> # A tibble: 324 × 7
#> starttime endtime site parameter unit value n
#> <dttm> <dttm> <chr> <chr> <chr> <dbl> <dbl>
#> 1 2018-01-03 00:00:00 2018-01-04 00:00:00 Zch_Stam… NO2_PS µg/m3 29.2 0.431
#> 2 2018-01-04 00:00:00 2018-01-05 00:00:00 Zch_Stam… NO2_PS µg/m3 29.2 1
#> 3 2018-01-05 00:00:00 2018-01-06 00:00:00 Zch_Stam… NO2_PS µg/m3 29.2 1
#> 4 2018-01-06 00:00:00 2018-01-07 00:00:00 Zch_Stam… NO2_PS µg/m3 29.2 1
#> 5 2018-01-07 00:00:00 2018-01-08 00:00:00 Zch_Stam… NO2_PS µg/m3 29.2 1
#> 6 2018-01-08 00:00:00 2018-01-09 00:00:00 Zch_Stam… NO2_PS µg/m3 29.2 1
#> 7 2018-01-09 00:00:00 2018-01-10 00:00:00 Zch_Stam… NO2_PS µg/m3 29.2 1
#> 8 2018-01-10 00:00:00 2018-01-11 00:00:00 Zch_Stam… NO2_PS µg/m3 29.2 1
#> 9 2018-01-11 00:00:00 2018-01-12 00:00:00 Zch_Stam… NO2_PS µg/m3 29.2 1
#> 10 2018-01-12 00:00:00 2018-01-13 00:00:00 Zch_Stam… NO2_PS µg/m3 29.2 1
#> # ℹ 314 more rows
wmean(data_ps, site, parameter, unit, interval = "1 week")
#> # A tibble: 49 × 7
#> starttime endtime site parameter unit value n
#> <dttm> <dttm> <chr> <chr> <chr> <dbl> <dbl>
#> 1 2017-12-31 00:00:00 2018-01-07 00:00:00 Zch_Stam… NO2_PS µg/m3 29.2 0.490
#> 2 2018-01-07 00:00:00 2018-01-14 00:00:00 Zch_Stam… NO2_PS µg/m3 29.2 1
#> 3 2018-01-14 00:00:00 2018-01-21 00:00:00 Zch_Stam… NO2_PS µg/m3 26.5 1
#> 4 2018-01-21 00:00:00 2018-01-28 00:00:00 Zch_Stam… NO2_PS µg/m3 25.1 1
#> 5 2018-01-28 00:00:00 2018-02-04 00:00:00 Zch_Stam… NO2_PS µg/m3 27.7 1
#> 6 2018-02-04 00:00:00 2018-02-11 00:00:00 Zch_Stam… NO2_PS µg/m3 29.0 1
#> 7 2018-02-11 00:00:00 2018-02-18 00:00:00 Zch_Stam… NO2_PS µg/m3 30.3 1
#> 8 2018-02-18 00:00:00 2018-02-25 00:00:00 Zch_Stam… NO2_PS µg/m3 31 1
#> 9 2018-02-25 00:00:00 2018-03-04 00:00:00 Zch_Stam… NO2_PS µg/m3 33.8 1
#> 10 2018-03-04 00:00:00 2018-03-11 00:00:00 Zch_Stam… NO2_PS µg/m3 35.4 1
#> # ℹ 39 more rows
wmean(data_ps, site, parameter, unit, interval = "m1")
#> # A tibble: 12 × 7
#> starttime endtime site parameter unit value n
#> <dttm> <dttm> <chr> <chr> <chr> <dbl> <dbl>
#> 1 2018-01-01 00:00:00 2018-02-01 00:00:00 Zch_Stam… NO2_PS µg/m3 27.2 0.917
#> 2 2018-02-01 00:00:00 2018-03-01 00:00:00 Zch_Stam… NO2_PS µg/m3 30.4 1
#> 3 2018-03-01 00:00:00 2018-04-01 00:00:00 Zch_Stam… NO2_PS µg/m3 31.9 1
#> 4 2018-04-01 00:00:00 2018-05-01 00:00:00 Zch_Stam… NO2_PS µg/m3 28.2 1
#> 5 2018-05-01 00:00:00 2018-06-01 00:00:00 Zch_Stam… NO2_PS µg/m3 20.6 0.548
#> 6 2018-06-01 00:00:00 2018-07-01 00:00:00 Zch_Stam… NO2_PS µg/m3 22.2 1
#> 7 2018-07-01 00:00:00 2018-08-01 00:00:00 Zch_Stam… NO2_PS µg/m3 23.1 1
#> 8 2018-08-01 00:00:00 2018-09-01 00:00:00 Zch_Stam… NO2_PS µg/m3 24.2 1
#> 9 2018-09-01 00:00:00 2018-10-01 00:00:00 Zch_Stam… NO2_PS µg/m3 32.2 1
#> 10 2018-10-01 00:00:00 2018-11-01 00:00:00 Zch_Stam… NO2_PS µg/m3 37.2 1
#> 11 2018-11-01 00:00:00 2018-12-01 00:00:00 Zch_Stam… NO2_PS µg/m3 30.2 0.536
#> 12 2018-12-01 00:00:00 2019-01-01 00:00:00 Zch_Stam… NO2_PS µg/m3 28.5 0.560
wmean(data_ps, site, parameter, unit, interval = "y1")
#> # A tibble: 1 × 7
#> starttime endtime site parameter unit value n
#> <dttm> <dttm> <chr> <chr> <chr> <dbl> <dbl>
#> 1 2018-01-01 00:00:00 2019-01-01 00:00:00 Zch_Stamp… NO2_PS µg/m3 28.2 0.879