
Last chance! 50% off unlimited learning
Sale ends in
index_by()
is the counterpart of group_by()
in temporal context, but it
only groups the time index. It adds a new column and then group it. The
following operation is applied to each group of the index, similar to
group_by()
but dealing with index only. index_by()
+ summarise()
will
update the grouping index variable to be the new index. Use ungroup()
or
index_by()
with no arguments to remove the index grouping vars.
index_by(.data, ...)
A tbl_ts
.
A single name-value pair of expression: a new index on LHS and the current index on RHS
An existing variable to be used as index The index functions that can be used, but not limited:
lubridate::year: yearly aggregation
yearquarter: quarterly aggregation
yearmonth: monthly aggregation
yearweek: weekly aggregation
as.Date or lubridate::as_date: daily aggregation
lubridate::ceiling_date, lubridate::floor_date, or lubridate::round_date: sub-daily aggregation
other index functions from other packages
A index_by()
-ed tsibble is indicated by @
in the "Groups" when
displaying on the screen.
Time index will not be collapsed by summarise.tbl_ts
.
The scoped variants of summarise()
only operate on the non-key and
non-index variables.
# NOT RUN {
# Monthly counts across sensors ----
monthly_ped <- pedestrian %>%
group_by(Sensor) %>%
index_by(Year_Month = yearmonth(Date_Time)) %>%
summarise(
Max_Count = max(Count),
Min_Count = min(Count)
)
monthly_ped
index(monthly_ped)
# Using existing variable ----
pedestrian %>%
group_by(Sensor) %>%
index_by(Date) %>%
summarise(
Max_Count = max(Count),
Min_Count = min(Count)
)
# Annual trips by Region and State ----
tourism %>%
index_by(Year = lubridate::year(Quarter)) %>%
group_by(Region, State) %>%
summarise(Total = sum(Trips))
# }
Run the code above in your browser using DataLab