Use a concise filtering method to filter a tbl_time object by its index.

filter_time(.tbl_time, time_formula)

# S3 method for tbl_time
[(x, i, j, drop = FALSE)

Arguments

.tbl_time

A tbl_time object.

time_formula

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

x

Same as .tbl_time but consistent naming with base R.

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 the character 'YYYY-MM-DD HH:MM:SS', but powerful shorthand is available. Some examples are:

The time_formula can also use a one sided formula.

The time_formula can also use 'start' and 'end' as keywords for your filter.

All shorthand dates are expanded:

  • 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 (assuming your index is a POSIXct):

Special parsing is done for indices of class hms. The from ~ to time formula is specified as only HH:MM:SS.

Subsecond resolution is also supported, however, R has a unique way of handling and printing subsecond dates and the user should be comfortable with this already. Specify subsecond resolution like so: '2013-01-01 00:00:00.1' ~ '2013-01-01 00:00:00.2'. Note that one sided expansion does not work with subsecond resolution due to seconds and subseconds being grouped together into 1 number (i.e. 1.2 seconds). This means ~'2013-01-01 00:00:00' does not expand to something like '2013-01-01 00:00:00.00' ~ '2013-01-01 00:00:00.99', but only expands to include whole seconds.

This function respects dplyr::group_by() groups.

Examples

# 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 filter_time(FANG, '2013' ~ '2014')
#> # A time tibble: 2,016 x 8 #> # Index: date #> # Groups: symbol [4] #> symbol date open high low close volume adjusted #> <chr> <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> #>  1 FB 2013-01-02 27.4 28.2 27.4 28 69846400 28 #>  2 FB 2013-01-03 27.9 28.5 27.6 27.8 63140600 27.8 #>  3 FB 2013-01-04 28.0 28.9 27.8 28.8 72715400 28.8 #>  4 FB 2013-01-07 28.7 29.8 28.6 29.4 83781800 29.4 #>  5 FB 2013-01-08 29.5 29.6 28.9 29.1 45871300 29.1 #>  6 FB 2013-01-09 29.7 30.6 29.5 30.6 104787700 30.6 #>  7 FB 2013-01-10 30.6 31.5 30.3 31.3 95316400 31.3 #>  8 FB 2013-01-11 31.3 32.0 31.1 31.7 89598000 31.7 #>  9 FB 2013-01-14 32.1 32.2 30.6 31.0 98892800 31.0 #> 10 FB 2013-01-15 30.6 31.7 29.9 30.1 173242600 30.1 #> # … with 2,006 more rows
# 2013-05-25 to 2014-06-04 filter_time(FANG, '2013-05-25' ~ '2014-06-04')
#> # A time tibble: 1,032 x 8 #> # Index: date #> # Groups: symbol [4] #> symbol date open high low close volume adjusted #> <chr> <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> #>  1 FB 2013-05-28 24.5 24.5 23.9 24.1 50079700 24.1 #>  2 FB 2013-05-29 23.8 23.8 23.3 23.3 64237800 23.3 #>  3 FB 2013-05-30 24.1 24.8 23.9 24.5 60733200 24.5 #>  4 FB 2013-05-31 24.6 25.0 24.3 24.4 35925000 24.4 #>  5 FB 2013-06-03 24.3 24.3 23.7 23.8 35733800 23.8 #>  6 FB 2013-06-04 23.9 23.9 23.3 23.5 34760800 23.5 #>  7 FB 2013-06-05 23.4 23.7 22.8 22.9 53819700 22.9 #>  8 FB 2013-06-06 23.0 23.1 22.7 23.0 31260700 23.0 #>  9 FB 2013-06-07 23.0 23.4 22.9 23.3 38699200 23.3 #> 10 FB 2013-06-10 24.1 24.6 24.0 24.3 58393000 24.3 #> # … with 1,022 more rows
# Using the `[` subset operator FANG['2014'~'2015']
#> # A time tibble: 2,016 x 8 #> # Index: date #> # Groups: symbol [4] #> symbol date open high low close volume adjusted #> <chr> <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> #>  1 FB 2014-01-02 54.8 55.2 54.2 54.7 43195500 54.7 #>  2 FB 2014-01-03 55.0 55.7 54.5 54.6 38246200 54.6 #>  3 FB 2014-01-06 54.4 57.3 54.0 57.2 68852600 57.2 #>  4 FB 2014-01-07 57.7 58.5 57.2 57.9 77207400 57.9 #>  5 FB 2014-01-08 57.6 58.4 57.2 58.2 56682400 58.2 #>  6 FB 2014-01-09 58.7 59.0 56.7 57.2 92253300 57.2 #>  7 FB 2014-01-10 57.1 58.3 57.1 57.9 42449500 57.9 #>  8 FB 2014-01-13 57.9 58.2 55.4 55.9 63010900 55.9 #>  9 FB 2014-01-14 56.5 57.8 56.1 57.7 37503600 57.7 #> 10 FB 2014-01-15 58.0 58.6 57.3 57.6 33663400 57.6 #> # … with 2,006 more rows
# Using `[` and one sided formula for only dates in 2014 FANG[~'2014']
#> # A time tibble: 1,008 x 8 #> # Index: date #> # Groups: symbol [4] #> symbol date open high low close volume adjusted #> <chr> <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> #>  1 FB 2014-01-02 54.8 55.2 54.2 54.7 43195500 54.7 #>  2 FB 2014-01-03 55.0 55.7 54.5 54.6 38246200 54.6 #>  3 FB 2014-01-06 54.4 57.3 54.0 57.2 68852600 57.2 #>  4 FB 2014-01-07 57.7 58.5 57.2 57.9 77207400 57.9 #>  5 FB 2014-01-08 57.6 58.4 57.2 58.2 56682400 58.2 #>  6 FB 2014-01-09 58.7 59.0 56.7 57.2 92253300 57.2 #>  7 FB 2014-01-10 57.1 58.3 57.1 57.9 42449500 57.9 #>  8 FB 2014-01-13 57.9 58.2 55.4 55.9 63010900 55.9 #>  9 FB 2014-01-14 56.5 57.8 56.1 57.7 37503600 57.7 #> 10 FB 2014-01-15 58.0 58.6 57.3 57.6 33663400 57.6 #> # … with 998 more rows
# Using `[` and column selection FANG['2013'~'2016', c("date", "adjusted")]
#> # A time tibble: 4,032 x 2 #> # Index: date #> date adjusted #> <date> <dbl> #>  1 2013-01-02 28 #>  2 2013-01-03 27.8 #>  3 2013-01-04 28.8 #>  4 2013-01-07 29.4 #>  5 2013-01-08 29.1 #>  6 2013-01-09 30.6 #>  7 2013-01-10 31.3 #>  8 2013-01-11 31.7 #>  9 2013-01-14 31.0 #> 10 2013-01-15 30.1 #> # … with 4,022 more rows
# Variables are unquoted using rlang lhs_date <- "2013" rhs_date <- as.Date("2014-01-01") filter_time(FANG, lhs_date ~ rhs_date)
#> # A time tibble: 1,008 x 8 #> # Index: date #> # Groups: symbol [4] #> symbol date open high low close volume adjusted #> <chr> <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> #>  1 FB 2013-01-02 27.4 28.2 27.4 28 69846400 28 #>  2 FB 2013-01-03 27.9 28.5 27.6 27.8 63140600 27.8 #>  3 FB 2013-01-04 28.0 28.9 27.8 28.8 72715400 28.8 #>  4 FB 2013-01-07 28.7 29.8 28.6 29.4 83781800 29.4 #>  5 FB 2013-01-08 29.5 29.6 28.9 29.1 45871300 29.1 #>  6 FB 2013-01-09 29.7 30.6 29.5 30.6 104787700 30.6 #>  7 FB 2013-01-10 30.6 31.5 30.3 31.3 95316400 31.3 #>  8 FB 2013-01-11 31.3 32.0 31.1 31.7 89598000 31.7 #>  9 FB 2013-01-14 32.1 32.2 30.6 31.0 98892800 31.0 #> 10 FB 2013-01-15 30.6 31.7 29.9 30.1 173242600 30.1 #> # … with 998 more rows
# Use the keywords 'start' and 'end' to conveniently access ends filter_time(FANG, 'start' ~ '2014')
#> # A time tibble: 2,016 x 8 #> # Index: date #> # Groups: symbol [4] #> symbol date open high low close volume adjusted #> <chr> <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> #>  1 FB 2013-01-02 27.4 28.2 27.4 28 69846400 28 #>  2 FB 2013-01-03 27.9 28.5 27.6 27.8 63140600 27.8 #>  3 FB 2013-01-04 28.0 28.9 27.8 28.8 72715400 28.8 #>  4 FB 2013-01-07 28.7 29.8 28.6 29.4 83781800 29.4 #>  5 FB 2013-01-08 29.5 29.6 28.9 29.1 45871300 29.1 #>  6 FB 2013-01-09 29.7 30.6 29.5 30.6 104787700 30.6 #>  7 FB 2013-01-10 30.6 31.5 30.3 31.3 95316400 31.3 #>  8 FB 2013-01-11 31.3 32.0 31.1 31.7 89598000 31.7 #>  9 FB 2013-01-14 32.1 32.2 30.6 31.0 98892800 31.0 #> 10 FB 2013-01-15 30.6 31.7 29.9 30.1 173242600 30.1 #> # … with 2,006 more rows
# hms (hour, minute, second) classes have special parsing hms_example <- create_series(~'12:01', 'second', class = 'hms') filter_time(hms_example, 'start' ~ '12:01:30')
#> # A time tibble: 31 x 1 #> # Index: date #> date #> <time> #>  1 12:01 #>  2 12:01 #>  3 12:01 #>  4 12:01 #>  5 12:01 #>  6 12:01 #>  7 12:01 #>  8 12:01 #>  9 12:01 #> 10 12:01 #> # … with 21 more rows