1- # ' Add Time Variables to a dataframe
1+ # ' Add Time Variables
22# '
33# ' This function adds multiple time-related variables to a dataframe with a Date column.
4- # ' It creates standard factors such as season, month-year, day-hour, and determines summer/winter seasonality.
5- # ' It also allows flexible specification of summer and winter start/end dates, and a custom time period.
4+ # ' It creates standard factors such as season, month-year, day-hour, and determines summer/winter.
5+ # ' It also allows flexible specification of summer and winter start/end dates,
6+ # ' and a custom time period.
7+ # '
8+ # ' @details
9+ # ' The variables \code{seasonyear}, \code{season}, \code{monthyear}, and \code{daylight}
10+ # ' are created using the \code{openair::cutData()}
11+ # ' function internally and rely on geographic coordinates (latitude, longitude)
12+ # ' to calculate daylight status.
13+ # ' Be sure \code{openair} is installed and loaded for these variables.
614# '
715# ' @param mydata A dataframe containing a date/time column labelled "Date" and "Sensor" column.
816# ' @param Date The name of the date/time column in `mydata` (default "Date").
1624# ' @param longitude Longitude for daylight calculations (default -0.5).
1725# ' @param ... Additional arguments passed to `openair::cutData()`.
1826# '
19- # ' @return Dataframe with appended columns:
20- # ' \describe{
21- # ' \item{seasonyear, season, monthyear, daylight}{Standard time factors as used in \code{openair::cutData()}.}
22- # ' \item{day, hour, dayhour, daymonth, month, year}{Time breakdowns for aggregation and analysis.}
23- # ' \item{Season}{Categorises row as "Summer" or "Winter" using user-specified thresholds.}
24- # ' \item{Period}{Flags whether each date falls within custom period (e.g. exhibition), with assigned label.}
25- # ' }
27+ # ' @return A data frame with additional time-related columns appended:
28+ # ' \describe{
29+ # ' \item{seasonyear}{Combined year and season factor created by \code{openair::cutData()};
30+ # ' useful for seasonal analyses.}
31+ # ' \item{season}{Season factor (e.g., Spring, Summer) from \code{openair::cutData()}.}
32+ # ' \item{monthyear}{Factor combining month and year, created by \code{openair::cutData()}
33+ # ' to assist month-based grouping.}
34+ # ' \item{daylight}{Boolean or factor indicating daylight presence/absence,
35+ # ' derived using \code{openair::cutData()} with latitude and longitude inputs.}
36+ # ' \item{day}{Date part of the timestamp, rounded down to day boundary, useful for daily aggregation.}
37+ # ' \item{hour}{Hour of the day extracted from the datetime.}
38+ # ' \item{dayhour}{Datetime floored to the hour; useful for hourly time series analysis.}
39+ # ' \item{weekday}{Weekday name/factor, abbreviated, extracted from the date.}
40+ # ' \item{month}{Month number and its labelled factor version; useful for calendar-based grouping.}
41+ # ' \item{year}{Year extracted from the datetime for annual analyses.}
42+ # ' \item{DayYear}{Date with the current year but month and day taken from the
43+ # ' original date; used to assign seasons and periods relative to current year.}
44+ # ' \item{Summer}{A factor ("Summer" or "Winter") determined by comparison of \code{DayYear}
45+ # ' with user-defined \code{summer_start} and \code{summer_end} dates,
46+ # ' for custom seasonality modelling.}
47+ # ' \item{Period}{Character flag identifying whether the date falls within a user-defined
48+ # ' custom period (e.g., an exhibition), labelled by \code{period_label}.
49+ # ' Returns \code{NA} if no period defined.}
50+ # ' }
2651# '
2752# '
28- # ' @importFrom lubridate floor_date hour month year day minute make_datetime make_date now as_date
29- # ' @importFrom dplyr mutate if_else
53+ # ' @importFrom lubridate floor_date hour month year day make_date now as_date wday
54+ # ' @importFrom dplyr mutate if_else rename
3055# ' @import openair
3156# ' @export
3257# '
@@ -51,7 +76,6 @@ add_time_vars <- function(
5176 ...
5277){
5378
54- # Ensure Date column is POSIXct
5579 mydata [[Date ]] <- as.POSIXct(mydata [[Date ]])
5680
5781 # Rename Date column to 'date' to match openair expectation
@@ -72,15 +96,16 @@ add_time_vars <- function(
7296 day = floor_date(date , unit = " day" ),
7397 hour = hour(date ),
7498 dayhour = floor_date(date , unit = " hour" ),
75- daymonth = day(date ),
99+ weekday = wday(date , label = TRUE , abbr = TRUE ),
100+ Month = month(date ),
76101 month = month(date , label = TRUE , abbr = TRUE ),
77102 year = year(date ),
78103 DayYear = make_date(
79104 year = year(now()),
80105 month = month(date ),
81106 day = day(date )
82107 ),
83- Season = if_else(
108+ Summer = if_else(
84109 DayYear > = as_date(paste(year(now()), summer_start , sep = " -" )) &
85110 DayYear < as_date(paste(year(now()), summer_end , sep = " -" )),
86111 " Summer" , " Winter"
0 commit comments