This function partitions a data frame up into different time segments. It
produces a new column called controlled by name that can be used in many
openair functions. Note that there must be one more labels than there are
dates.
Arguments
- mydata
A data frame containing a
datefield in an hourly or high resolution format.- dates
A date or dates to split data by. Can be passed as R date(time) objects or as characters. If passed as a character,
splitByDate()expects either "DD/MM/YYYY" or "YYYY/MM/DD" by default, but this can be chaned using theformatargument.- labels
Labels for each time partition. Should always be one more
labelthan there aredates; for example, ifdates = "2020/01/01,splitByDate()requires one label for before that date and one label for after.- name
The name to give the new column to identify the periods split. Defaults to
"split.by".- format
When
datesare provided as character strings, this option defines the formatssplitByDate()will use to coercedatesinto RDateorPOSIXCtobjects. Passed tolubridate::as_date()orlubridate::as_datetime(). Seestrptime()for more information.
Examples
# split data up into "before" and "after"
mydata <- splitByDate(mydata,
dates = "1/04/2000",
labels = c("before", "after")
)
# split data into 3 partitions
mydata <- splitByDate(mydata,
dates = c("1/1/2000", "1/3/2003"),
labels = c("before", "during", "after")
)
# if you have modelled data - could split into modelled and measured by the
# break date
dummy <- data.frame(
date = Sys.Date() + (-5:5),
nox = 100 + seq(-50, 50, 10)
)
splitByDate(dummy,
dates = Sys.Date(),
labels = c("measured", "modelled"),
name = "data_type"
)
#> date nox data_type
#> 1 2025-10-12 50 measured
#> 2 2025-10-13 60 measured
#> 3 2025-10-14 70 measured
#> 4 2025-10-15 80 measured
#> 5 2025-10-16 90 measured
#> 6 2025-10-17 100 measured
#> 7 2025-10-18 110 modelled
#> 8 2025-10-19 120 modelled
#> 9 2025-10-20 130 modelled
#> 10 2025-10-21 140 modelled
#> 11 2025-10-22 150 modelled
