View source: R/dplyr-slice_period.R
slice_period | R Documentation |
Applies a dplyr slice inside a time-based period (window).
slice_period(.data, ..., .date_var, .period = "1 day")
.data |
A |
... |
For 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 |
.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 The value can be:
Arbitrary unique English abbreviations as in the
|
A tibble
or data.frame
Time-Based dplyr functions:
summarise_by_time()
- Easily summarise using a date column.
mutate_by_time()
- Simplifies applying mutations by time windows.
pad_by_time()
- Insert time series rows with regularly spaced timestamps
filter_by_time()
- Quickly filter using date ranges.
filter_period()
- Apply filtering expressions inside periods (windows)
slice_period()
- Apply slice inside periods (windows)
condense_period()
- Convert to a different periodicity
between_time()
- Range detection for date or date-time sequences.
slidify()
- Turn any function into a sliding (rolling) function
# Libraries
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")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.