time_filter: Succinctly filter a 'tbl_time' object by date

Description Usage Arguments Details Examples

View source: R/time_filter.R

Description

Use a concise filtering method to filter a ‘tbl_time' object by it’s 'index'.

Usage

1
2
3
4
time_filter(x, time_formula)

## S3 method for class 'tbl_time'
x[i, j, drop = FALSE]

Arguments

x

A 'tbl_time' object.

time_formula

A period to filter over. This is specified as a 'formula'. See 'Details'.

i

A period to filter over. This is specified the same as 'time_formula' or can use the traditional row extraction method.

j

Optional argument to also specify column index to subset. Works exactly like the normal extraction operator.

drop

Will always be coerced to 'FALSE' by 'tibble'.

Details

The 'time_formula' is specified using the format 'from ~ to'. Each side of the 'time_formula' is specified as 'YYYY-MM-DD + HH:MM:SS', but powerful shorthand is available. Some examples are: * __Year:__ '2013 ~ 2015' * __Month:__ '2013-01 ~ 2016-06' * __Day:__ '2013-01-05 ~ 2016-06-04' * __Second:__ '2013-01-05 + 10:22:15 ~ 2018-06-03 + 12:14:22' * __Variations:__ '2013 ~ 2016-06'

The 'time_formula' can also use a one sided formula. * __Only dates in 2015:__ '~2015' * __Only dates March 2015:__ '~2015-03'

All shorthand dates are expanded to be valid POSIXct dates. * The 'from' side is expanded to be the first date in that period * The 'to' side is expanded to be the last date in that period

This means that the following examples are equivalent: * '2015 ~ 2016 == 2015-01-01 + 00:00:00 ~ 2016-12-31 + 23:59:59' * '~2015 == 2015-01-01 + 00:00:00 ~ 2015-12-31 + 23:59:59' * '2015-01-04 + 10:12 ~ 2015-01-05 == 2015-01-04 + 10:12:00 ~ 2015-01-05 + 23:59:59'

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
24
# FANG contains Facebook, Amazon, Netflix and Google stock prices
data(FANG)
FANG <- as_tbl_time(FANG, date) %>%
  dplyr::group_by(symbol)

# 2013-01-01 to 2014-12-31
time_filter(FANG, 2013 ~ 2014)

# 2013-05-25 to 2014-06-04
time_filter(FANG, 2013-05-25 ~ 2014-06-04)

# Using the `[` subset operator
FANG[2014~2015]

# Using `[` and one sided formula for only dates in 2014
FANG[~2014]

# Using `[` and column selection
FANG[2013~2016, c("date", "adjusted")]

# You can filter with variables
lhs_date <- "2013"
rhs_date <- as.Date("2014-01-01")
time_filter(FANG, lhs_date ~ rhs_date)

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