Appends new factor variables as columns to the dataset, for further use
with e.g. summary_periodic()
in order to calculate summary stats for
starttime of the day, date, weekday, weekend, week, month, season, daylight.
Usage
cut_timeseries_periodic(
data,
x = "starttime",
include_daylight = TRUE,
coords = c(lat = 47.36667, lon = 8.55)
)
Arguments
- data
tibble containing a column starttime of type POSIXct with a timezone
- x
symbolic reference to date time column used for cutting
- include_daylight
boolean indicating to calculate daylight for each starttime. Disable to improve perfomance.
- coords
a named vector of the location in WGS84 coordinates for the daylight factoring.
suncalc::getSunlightTimes()
is used to calculate sunrise, sunset times. Default: c(lat = 47.36667, lon = 8.55) => Zuerich, Switzerland
Value
a tibble with cut data; cut-factors comprise various (time-)periodic new columns:
starttime_of_day:
cut_time_of_day(x)
date:
lubridate::as_date(x)
to preserve time zone don't usebase::as.Date()
weekday:
lubridate::wday(x, label = TRUE, week_start = 1)
locale Abbreviations (Mon, Tue, Wed, Thu, Fri, Sat, Sun)weekend:
cut_weekend(x)
=> Wochentag, Wochenende (usecut_weekend()
manually or recode factor to change levels)week:
lubridate::week(x)
month:
lubridate::month(x, label = TRUE)
locale Abbreviations (Jan, Feb, Mar, ...)season:
cut_season(x)
=> DJF, MAM, JJA, SON (usecut_season()
manually or recode factor to change levels)daylight:
cut_daylight(x, coords)
=> Tag, Nacht (usecut_daylight()
manually or recode factor to change levels)
Examples
fn <- rOstluft.data::f("Zch_Stampfenbachstrasse_h1_2013_Jan.csv")
data <- rOstluft::read_airmo_csv(fn)
data <- cut_timeseries_periodic(data)
tibble::glimpse(data)
#> Rows: 14,116
#> Columns: 14
#> $ starttime <dttm> 2013-01-01 00:00:00, 2013-01-01 01:00:00, 2013-01-01…
#> $ site <fct> Zch_Stampfenbachstrasse, Zch_Stampfenbachstrasse, Zch…
#> $ parameter <fct> CO, CO, CO, CO, CO, CO, CO, CO, CO, CO, CO, CO, CO, C…
#> $ interval <fct> h1, h1, h1, h1, h1, h1, h1, h1, h1, h1, h1, h1, h1, h…
#> $ unit <fct> mg/m3, mg/m3, mg/m3, mg/m3, mg/m3, mg/m3, mg/m3, mg/m…
#> $ value <dbl> 0.8051369, 0.8326345, 0.8742825, 0.7549847, 0.7667311…
#> $ starttime_of_day <ord> 00:00, 01:00, 02:00, 03:00, 04:00, 05:00, 06:00, 07:0…
#> $ date <date> 2013-01-01, 2013-01-01, 2013-01-01, 2013-01-01, 2013…
#> $ weekday <ord> Di\., Di\., Di\., Di\., Di\., Di\., Di\., Di\., Di\.,…
#> $ weekend <ord> Wochentag, Wochentag, Wochentag, Wochentag, Wochentag…
#> $ week <dbl> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,…
#> $ month <ord> Jan, Jan, Jan, Jan, Jan, Jan, Jan, Jan, Jan, Jan, Jan…
#> $ season <ord> DJF, DJF, DJF, DJF, DJF, DJF, DJF, DJF, DJF, DJF, DJF…
#> $ daylight <ord> Nacht, Nacht, Nacht, Nacht, Nacht, Nacht, Nacht, Nach…
# recoding a factor
data <- dplyr::mutate(data,
daylight = dplyr::recode(daylight, Nacht = "night", Tag = "day"),
)
tibble::glimpse(data)
#> Rows: 14,116
#> Columns: 14
#> $ starttime <dttm> 2013-01-01 00:00:00, 2013-01-01 01:00:00, 2013-01-01…
#> $ site <fct> Zch_Stampfenbachstrasse, Zch_Stampfenbachstrasse, Zch…
#> $ parameter <fct> CO, CO, CO, CO, CO, CO, CO, CO, CO, CO, CO, CO, CO, C…
#> $ interval <fct> h1, h1, h1, h1, h1, h1, h1, h1, h1, h1, h1, h1, h1, h…
#> $ unit <fct> mg/m3, mg/m3, mg/m3, mg/m3, mg/m3, mg/m3, mg/m3, mg/m…
#> $ value <dbl> 0.8051369, 0.8326345, 0.8742825, 0.7549847, 0.7667311…
#> $ starttime_of_day <ord> 00:00, 01:00, 02:00, 03:00, 04:00, 05:00, 06:00, 07:0…
#> $ date <date> 2013-01-01, 2013-01-01, 2013-01-01, 2013-01-01, 2013…
#> $ weekday <ord> Di\., Di\., Di\., Di\., Di\., Di\., Di\., Di\., Di\.,…
#> $ weekend <ord> Wochentag, Wochentag, Wochentag, Wochentag, Wochentag…
#> $ week <dbl> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,…
#> $ month <ord> Jan, Jan, Jan, Jan, Jan, Jan, Jan, Jan, Jan, Jan, Jan…
#> $ season <ord> DJF, DJF, DJF, DJF, DJF, DJF, DJF, DJF, DJF, DJF, DJF…
#> $ daylight <ord> night, night, night, night, night, night, night, nigh…