filter_Datetime {LightLogR} | R Documentation |
Filter Datetimes in a dataset.
Description
Filtering a dataset based on Dates or Datetimes may often be necessary prior
to calcuation or visualization. The functions allow for a filtering based on
simple strings
or Datetime
scalars, or by specifying a length. They also
support prior dplyr grouping, which is useful, e.g., when you only want to
filter the first two days of measurement data for every participant,
regardless of the actual date. If you want to filter based on times of the
day, look to filter_Time()
.
Usage
filter_Datetime(
dataset,
Datetime.colname = Datetime,
start = NULL,
end = NULL,
length = NULL,
length_from_start = TRUE,
full.day = FALSE,
tz = NULL,
only_Id = NULL,
filter.expr = NULL
)
filter_Date(..., start = NULL, end = NULL)
Arguments
dataset |
A light logger dataset. Expects a |
Datetime.colname |
column name that contains the datetime. Defaults to
|
start , end |
For
|
length |
Either a Period or Duration from lubridate. E.g., |
length_from_start |
A |
full.day |
A |
tz |
Timezone of the start/end times. If |
only_Id |
An expression of |
filter.expr |
Advanced filtering conditions. If not |
... |
Parameter handed over to |
Value
a data.frame
object identical to dataset
but with only the
specified Dates/Times.
See Also
Other filter:
filter_Time()
Other filter:
filter_Time()
Examples
library(lubridate)
library(dplyr)
#baseline
range.unfiltered <- sample.data.environment$Datetime %>% range()
range.unfiltered
#setting the start of a dataset
sample.data.environment %>%
filter_Datetime(start = "2023-08-18 12:00:00") %>%
pull(Datetime) %>%
range()
#setting the end of a dataset
sample.data.environment %>%
filter_Datetime(end = "2023-08-18 12:00:00") %>% pull(Datetime) %>% range()
#setting a period of a dataset
sample.data.environment %>%
filter_Datetime(end = "2023-08-18 12:00:00", length = days(2)) %>%
pull(Datetime) %>% range()
#setting only the period of a dataset
sample.data.environment %>%
filter_Datetime(length = days(2)) %>%
pull(Datetime) %>% range()
#advanced filtering based on grouping (second day of each group)
sample.data.environment %>%
#shift the "Environment" group by one day
mutate(
Datetime = ifelse(Id == "Environment", Datetime + ddays(1), Datetime) %>%
as_datetime()) -> sample
sample %>% summarize(Daterange = paste(min(Datetime), max(Datetime), sep = " - "))
#now we can use the `filter.expr` argument to filter from the second day of each group
sample %>%
filter_Datetime(filter.expr = Datetime > Datetime[1] + days(1)) %>%
summarize(Daterange = paste(min(Datetime), max(Datetime), sep = " - "))
sample.data.environment %>% filter_Date(end = "2023-08-17")