cross_by_periods: Expand a table so that it can be aggregated by a period

Description Usage Arguments Value Examples

View source: R/cross-periods.R

Description

Cross by any set of calendar periods (like day or week), rolling windows, or recent intervals (like "4 Weeks", or "8 Weeks"). This means that each row in the input will appear potentially multiple times, each time associated with a different period and date.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
cross_by_periods(tbl, periods, windows, intervals, ...)

## S3 method for class 'tbl_lazy'
cross_by_periods(
  tbl,
  periods = c("week", "month", "quarter"),
  windows = c(),
  intervals = FALSE,
  remote_date_periods = NULL,
  ...
)

## S3 method for class 'tbl_df'
cross_by_periods(
  tbl,
  periods = c("week", "month", "quarter"),
  windows = c(),
  intervals = FALSE,
  ...
)

cross_by_periods_cumulative(tbl, remote_date_periods = NULL)

Arguments

tbl

A tbl, either local or remote.

periods

A vector of calendar periods. This supports "day", "week", "month", "quarter", and "year".

windows

A vector of windows, each representing a # of days

intervals

Whether a preselected set of intervals starting from today, such as "Last Week", "Last 2 Weeks", or "All Time" should be included.

...

Extra arguments, not used

remote_date_periods

For crossing remote tables, an existing remote table linking dates to their respective periods. By default, use a global accessor function.

Value

A tbl (either local or remote, depending on the input), where TODO. It is grouped by any grouping columns that were in the input, as well as by the new date and period columns.

Examples

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

flights <- nycflights13::flights %>%
  mutate(date = as.Date(ISOdate(year, month, day)))

# find flight delays by week, month, and quarter
flight_summary <- flights %>%
  cross_by_periods() %>%
  summarize(
    nb_flights = n(),
    avg_arr_delay = mean(arr_delay, na.rm = TRUE)
  )

library(ggplot2)

ggplot(flight_summary, aes(date, avg_arr_delay, color = period)) +
  geom_line()

datacamp/tidymetrics documentation built on March 21, 2021, 3:28 a.m.