Skip to content

Commit 85ee878

Browse files
committed
edit
1 parent 39cb96b commit 85ee878

File tree

5 files changed

+76
-27
lines changed

5 files changed

+76
-27
lines changed

NAMESPACE

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,10 @@ importFrom(lubridate,dmy_hm)
8080
importFrom(lubridate,floor_date)
8181
importFrom(lubridate,hour)
8282
importFrom(lubridate,make_date)
83-
importFrom(lubridate,make_datetime)
84-
importFrom(lubridate,minute)
8583
importFrom(lubridate,month)
8684
importFrom(lubridate,now)
8785
importFrom(lubridate,parse_date_time)
86+
importFrom(lubridate,wday)
8887
importFrom(lubridate,year)
8988
importFrom(padr,pad)
9089
importFrom(readr,cols)

R/add_time_vars.R

Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
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").
@@ -16,17 +24,34 @@
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"
-2.04 KB
Loading

man/add_time_vars.Rd

Lines changed: 35 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/figures/logo.png

-2.76 KB
Loading

0 commit comments

Comments
 (0)