time_group: Add time-based groupings to a tibble

Description Usage Arguments Details See Also Examples

View source: R/time_group.R

Description

[time_group()] accepts a date index vector and returns an integer vector that can be used for grouping by periods.

Usage

1
2
3
time_group(x, period = "yearly", start_date = NULL, ...)

make_time_group_vector(index_col, period, start_date = NULL, ...)

Arguments

period

A formula or character specification used for time-based grouping.

If a formula, e.g. '1~year', the formula is split and parsed to form the grouping period. The 'period' argument accepts a formula of the form 'multiple ~ period' allowing for flexible period grouping. The following are examples:

* 1 Year: '1~y' * 3 Months: '3~m' * 90 Days: '90~d'

Note that while shorthand is used above, an attempt is made to recognize more explicit period names such as:

* 2 Year: '2~year' / '2~years' / '2~yearly'

The 'period' argument also accepts characters that are converted to their corresponding periods. The following are accepted:

* '"yearly"' or '"y"' * '"quarterly"' or '"q"' * '"monthly"' or '"m"' * '"weekly"' or '"w"' * '"daily"' or '"d"' * '"hour"' or '"h"' * '"minute"' or '"M"' * '"second"' or '"s"'

start_date

Optional argument used to specify the start date for the first group. The default is to start at the closest period boundary below the minimum date in the supplied index.

...

Not currently used.

index

A vector of date indices to create groups for.

Details

This function is used internally, but may provide the user extra flexibility when they need to perform a grouped operation not supported by 'tibbletime'.

Grouping can only be done on the minimum periodicity of the index and above. This means that a daily series cannot be grouped by minute. An hourly series cannot be grouped by 5 seconds, and so on. If the user attempts this, groups will be returned at the minimum periodicity (a daily series will return 1 group per day).

The 'start_date' argument allows the user to control where the periods begin.

This function respects [dplyr::group_by()] groups.

See Also

[as_period()], [create_series()]

Examples

1
2
3
4
5
data(FB)

time_group(FB$date, 2~y)

dplyr::mutate(FB, time_group = time_group(date, 2~d))

DavisVaughan/tibbletime3 documentation built on May 28, 2019, 12:25 p.m.