collapse_index {tibbletime}R Documentation

Collapse an index vector so that all observations in an interval share the same date

Description

When collapse_index() is used, the index vector is altered so that all dates that fall in a specified interval share a common date. The most common use case for this is to then group on the collapsed index.

Usage

collapse_index(
  index,
  period = "year",
  start_date = NULL,
  side = "end",
  clean = FALSE,
  ...
)

Arguments

index

An index vector.

period

A character specification used for time-based grouping. The general format to use is "frequency period" where frequency is a number like 1 or 2, and period is an interval like weekly or yearly. There must be a space between the two.

Note that you can pass the specification in a flexible way:

  • 1 Year: '1 year' / '1 Y'

This shorthand is available for year, quarter, month, day, hour, minute, second, millisecond and microsecond periodicities.

Additionally, you have the option of passing in a vector of dates to use as custom and more flexible boundaries.

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.

side

Whether to return the date at the beginning or the end of the new period. By default, the "end" of the period. Use "start" to change to the start of the period.

clean

Whether or not to round the collapsed index up / down to the next period boundary. The decision to round up / down is controlled by the side argument.

...

Not currently used.

Details

The collapse_by() function provides a shortcut for the most common use of collapse_index(), calling the function inside a call to mutate() to modify the index directly. For more flexibility, like the nesting example below, use collapse_index().

Because this is often used for end of period summaries, the default is to use side = "end". Note that this is the opposite of as_period() where the default is side = "start".

The clean argument is especially useful if you have an irregular series and want cleaner dates to report for summary values.

Examples


# Basic functionality -------------------------------------------------------

# Facebook stock prices
data(FB)
FB <- as_tbl_time(FB, date)

# Collapse to weekly dates
dplyr::mutate(FB, date = collapse_index(date, "weekly"))

# A common workflow is to group on the new date column
# to perform a time based summary
FB %>%
  dplyr::mutate(date = collapse_index(date, "year")) %>%
  dplyr::group_by(date) %>%
  dplyr::summarise_if(is.numeric, mean)

# You can also assign the result to a separate column and use that
# to nest on, allowing for 'period nests' that keep the
# original dates in the nested tibbles.
FB %>%
  dplyr::mutate(nest_date = collapse_index(date, "2 year")) %>%
  dplyr::group_by(nest_date) %>%
  tidyr::nest()

# Grouped functionality -----------------------------------------------------

data(FANG)
FANG <- FANG %>%
  as_tbl_time(date) %>%
  dplyr::group_by(symbol)

# Collapse each group to monthly,
# calculate monthly standard deviation for each column
FANG %>%
  dplyr::mutate(date = collapse_index(date, "month")) %>%
  dplyr::group_by(symbol, date) %>%
  dplyr::summarise_all(sd)



[Package tibbletime version 0.1.8 Index]