filter_period: Apply filtering expressions inside periods (windows)

Description Usage Arguments Value See Also Examples

View source: R/dplyr-filter_period.R

Description

Applies a dplyr filtering expression inside a time-based period (window). See filter_by_time() for filtering continuous ranges defined by start/end dates. filter_period() enables filtering expressions like:

Usage

1
filter_period(.data, ..., .date_var, .period = "1 day")

Arguments

.data

A tbl object or data.frame

...

Filtering expression. Expressions that return a logical value, and are defined in terms of the variables in .data. If multiple expressions are included, they are combined with the & operator. Only rows for which all conditions evaluate to TRUE are kept.

.date_var

A column containing date or date-time values. If missing, attempts to auto-detect date column.

.period

A period to filter within. Time units are grouped using lubridate::floor_date() or lubridate::ceiling_date().

The value can be:

  • second

  • minute

  • hour

  • day

  • week

  • month

  • bimonth

  • quarter

  • season

  • halfyear

  • year

Arbitrary unique English abbreviations as in the lubridate::period() constructor are allowed:

  • "1 year"

  • "2 months"

  • "30 seconds"

Value

A tibble or data.frame

See Also

Time-Based dplyr functions:

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
# Libraries
library(timetk)
library(dplyr)

# Max value in each month
m4_daily %>%
    group_by(id) %>%
    filter_period(.period = "1 month", value == max(value))

# First date each month
m4_daily %>%
    group_by(id) %>%
    filter_period(.period = "1 month", date == first(date))

# All observations that are greater than a monthly average
m4_daily %>%
    group_by(id) %>%
    filter_period(.period = "1 month", value > mean(value))

timetk documentation built on Jan. 19, 2021, 1:06 a.m.