group_by_period: Aggregate a numeric column over a specified time grouping.

Description Usage Arguments Examples

Description

group_by_period takes a date column and a numeric column and aggregates the numeric column over specified date period. This is a common pattern in data analysis in business where we want to convert transactional data to daily, weekly or monthly data.

Usage

1
2
group_by_period(.data, .date_col, .summarise_col, unit, agg_fun = sum,
  na.rm = FALSE)

Arguments

.data

A data.frame to apply group_by_period to

.date_col

A date column that will be used for grouping the date. This should be of type date or datetime

.summarise_col

The column which will be aggregated

unit

A character string specifying a time unit or a multiple of a unit to be rounded to. See ?lubridate::floor_date for more details

agg_fun

A function used to aggregate the numeric column

na.rm

NA handling - this is passed to the na.rm argument of the aggregating function

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
library(nycflights13)
library(tidyr)
library(dplyr)

data("flights")

flights_w_date <- flights %>%
  unite(flight_date, year, month, day, sep = "-") %>%
  mutate(flight_date = ymd(flight_date))

flights_w_date %>%
  group_by_period(flight_date, distance, "month")

louis-vines/tidyutils documentation built on May 21, 2019, 12:05 p.m.