time_summarise: Summarise a 'tbl_time' by period

Description Usage Arguments Details Examples

View source: R/time_summarise.R

Description

[time_summarise()] works similarly to [dplyr::summarise()] but with the added benefit of being able to summarise by a time period such as '"yearly"' or '"monthly"'.

Usage

1
2
3
time_summarise(.data, period = "yearly", ..., start_date = NULL)

time_summarize(.data, period = "yearly", ..., start_date = NULL)

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

...

Not currently used.

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.

Details

Groups applied using [dplyr::group_by()] are respected.

In [dplyr::summarise()], one level of grouping is usually removed. Because an added group for the time index is added in 'time_summarise', none of the original groups are removed.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# Basic functionality -------------------------------------------------------

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

# Calculate the mean and standard deviation of the adjusted column
# at a yearly interval
FB %>%
  time_summarise(period = "yearly",
                 adj_mean = mean(adjusted),
                 adj_sd   = sd(adjusted))

# Want a more granular view? Look at monthly instead
FB %>%
  time_summarise(period = "monthly",
                 adj_mean = mean(adjusted),
                 adj_sd   = sd(adjusted))

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

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

# Groups are respected, allowing for very useful summaries
# grouped by symbol and by time period
FANG %>%
  time_summarise(period = "yearly",
                 vol_max   = max(volume),
                 vol_min   = min(volume),
                 # Like summarise, you can use columns you just computed
                 vol_range = vol_max - vol_min)

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