Learn R Programming

tsibble (version 0.5.3)

index_by: Group and collapse by time index

Description

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.

Usage

index_by(.data, ...)

Arguments

.data

A tbl_ts.

...

Details

  • A index_by()-ed tsibble is indicated by @ in the "Groups" when displaying on the screen.

Examples

Run this code
# 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