block()
breaks up the i
ndex by period
, and then uses that to define
the indices to chop x
with.
For example, it can split x
into monthly or yearly blocks. Combined with
purrr::map()
, it is a way to iterate over a vector in "time blocks".
block(x, i, period, every = 1L, origin = NULL)
x 
The vector to block. 

i 
The datetime index to block by. There are 3 restrictions on the index:

period 
A string defining the period to group by. Valid inputs can be roughly broken into:

every 
The number of periods to group together. For example, if the period was set to 
origin 
The reference date time value. The default when left as This is generally used to define the anchor time to count from, which is
relevant when the every value is 
A vector fulfilling the following invariants:
vec_size(block(x)) == vec_size(unique(warp::warp_boundary(i)))
vec_ptype(block(x)) == list()
vec_ptype(block(x)[[1]]) == vec_ptype(x)
block()
determines the indices to block by with warp::warp_boundary()
,
and splits x
by those indices using vctrs::vec_chop()
.
Like slide()
, block()
splits data frame x
values row wise.
#> [[1]] #> [1] "20181230" "20181231" #> #> [[2]] #> [1] "20190101" "20190102" "20190103" "20190201" #>#> [[1]] #> x i #> 1 1 20181230 #> 2 2 20181231 #> #> [[2]] #> x i #> 1 3 20190101 #> 2 4 20190102 #> 3 5 20190103 #> #> [[3]] #> x i #> 1 6 20190201 #># Iterate over these blocks to apply a function over # nonoverlapping period blocks. For example, to compute a # mean over yearly or monthly blocks. vapply(block(x, i, "year"), mean, numeric(1))#> [1] 1.5 4.5#> [1] 1.5 4.0 6.0# block by every 2 months, ensuring that we start counting # the 1st of the 2 months from `20190101` block(i, i, period = "month", every = 2, origin = as.Date("20190101"))#> [[1]] #> [1] "20181230" "20181231" #> #> [[2]] #> [1] "20190101" "20190102" "20190103" "20190201" #># Use the `origin` to instead start counting from `20181201`, meaning # that [201812, 201901] gets bucketed together. block(i, i, period = "month", every = 2, origin = as.Date("20181201"))#> [[1]] #> [1] "20181230" "20181231" "20190101" "20190102" "20190103" #> #> [[2]] #> [1] "20190201" #>