time_nest: Nest a 'tbl_time' to a specified time period

Description Usage Arguments Details Examples

View source: R/time_nest.R

Description

'time_nest' allows for quick nesting of 'tbl_time' objects by period so that manipulation can be performed on each period's data set individually. See [tidyr::nest()] for information on nesting generally.

Usage

1
2
time_nest(data, period = "yearly", ..., .key = "data",
  keep_inner_dates = TRUE)

Arguments

data

A 'tbl_time' object.

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"'

...

Used to specify columns you do not want in the nest. Specified as '-col_name'.

.key

The name of the new column, as a string or symbol.

This argument is passed by expression and supports quasiquotation (you can unquote strings and symbols). The name is captured from the expression with rlang::quo_name() (note that this kind of interface where symbols do not represent actual objects is now discouraged in the tidyverse; we support it here for backward compatibility).

keep_inner_dates

Whether to add dates to each nested object as the column, '.date'.

Details

As an example, nesting by a '"yearly"' period will return a 'tbl_time' object with a date column containing the dates at the end of each year, and a list-column of smaller 'tbl_time' objects containing all of the data for that year.

Because it is likely the case that the original dates will be required inside the nested 'tbl_time' objects for further manipulation, the original dates are kept as a '.date' column in each 'tbl_time' object in the list-column.

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

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# Basic functionality -------------------------------------------------------

data(FB)
FB <- as_tbl_time(FB, date)

# Nest yearly
time_nest(FB, "yearly")

# Nest yearly, but don't drag symbol into the nest
time_nest(FB, "yearly", -symbol)

# Nest quarterly
time_nest(FB, "quarterly")

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

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

# Nest yearly, but by group
FANG %>%
  time_nest("yearly")

DavisVaughan/tibbletime3 documentation built on Nov. 23, 2017, 5:30 p.m.