slice_period: Apply slice inside periods (windows)

Description Usage Arguments Value See Also Examples

View source: R/dplyr-slice_period.R

Description

Applies a dplyr slice inside a time-based period (window).

Usage

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

Arguments

.data

A tbl object or data.frame

...

For slice(): <data-masking> Integer row values.

Provide either positive values to keep, or negative values to drop. The values provided must be either all positive or all negative. Indices beyond the number of rows in the input are silently ignored.

For slice_helpers(), these arguments are passed on to methods.

.date_var

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

.period

A period to slice 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
# Libraries
library(timetk)
library(dplyr)

# First 5 observations in each month
m4_daily %>%
    group_by(id) %>%
    slice_period(1:5, .period = "1 month")

# Last observation in each month
m4_daily %>%
    group_by(id) %>%
    slice_period(n(), .period = "1 month")

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