as_period: Change 'tbl_time' periodicity

Description Usage Arguments Details Examples

View source: R/as_period.R

Description

Convert a 'tbl_time' object from daily to monthly, from minute data to hourly, and more. This allows the user to easily aggregate data to a less granular level.

Usage

1
as_period(x, period = "yearly", side = "start", start_date = NULL)

Arguments

x

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

side

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

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

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

Currently periods finer than second data are not supported.

The 'side' argument is useful when you want to return data at, say, the end of a quarter, or the end of a month.

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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# Basic usage ---------------------------------------------------------------

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

# Aggregate FB to yearly data
as_period(FB, "yearly")

# Aggregate FB to yearly data using a period formula
as_period(FB, 1~y)

# Aggregate FB to yearly data, but use the last data point available
# in that period
as_period(FB, "yearly", "end")

# Aggregate to weekly. Notice that it only uses the earliest day available
# in the data set at that periodicity. It will not set the date of the first
# row to 2013-01-01 because that date did not exist in the original data set.
as_period(FB, "weekly")

# Aggregate to every other week
as_period(FB, 2~w)

# FB is daily data, aggregate to minute?
# Does nothing and returns the original data set.
as_period(FB, "minute")

# Grouped usage -------------------------------------------------------------

# FANG contains Facebook, Amazon, Netflix and Google stock prices
data(FANG)
FANG <- as_tbl_time(FANG, date)

FANG <- FANG %>% dplyr::group_by(symbol)

# Respects groups
FANG %>% as_period("yearly")

# Every 6 months, respecting groups
as_period(FANG, 6~m)

# Using start_date ----------------------------------------------------------

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

# The Facebook series starts at 2013-01-02 so the 'every 2 day' counter
# starts at that date as well. Groups become (2013-01-02, 2013-01-03),
# (2013-01-04, 2013-01-05) and so on.
as_period(FB, 2~d)

# Specifying the `start_date = "2013-01-01"` might be preferable.
# Groups become (2013-01-01, 2013-01-02), (2013-01-03, 2013-01-04) and so on.
as_period(FB, 2~d, start_date = "2013-01-01")

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