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)
A tbl_time
object.
A period to filter over.
This is specified as a formula
. See Details
.
Same as .tbl_time
but consistent naming with base R.
A period to filter over. This is specified the same as
time_formula
or can use the traditional row extraction method.
Optional argument to also specify column index to subset. Works exactly like the normal extraction operator.
Will always be coerced to FALSE
by tibble
.
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:
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'
The time_formula
can also use 'start'
and 'end'
as keywords for
your filter.
Start of the series to end of 2015: 'start' ~ '2015'
Start of 2014 to end of series: '2014' ~ 'end'
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):
'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'
Special parsing is done for indices of class hms
. The from ~ to
time
formula is specified as only HH:MM:SS
.
Start to 5th second of the 12th hour: 'start' ~ '12:00:05'
Every second in the 12th hour: ~'12'
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.
# 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 × 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 × 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 × 8
#> # Index: date
#> 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 × 8
#> # Index: date
#> 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 × 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 × 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 × 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 × 1
#> # Index: date
#> date
#> <time>
#> 1 12:01:00
#> 2 12:01:01
#> 3 12:01:02
#> 4 12:01:03
#> 5 12:01:04
#> 6 12:01:05
#> 7 12:01:06
#> 8 12:01:07
#> 9 12:01:08
#> 10 12:01:09
#> # … with 21 more rows