Learn R Programming

tsibble (version 0.6.2)

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.

...

A single name-value pair of expression: a new index on LHS and the current index on RHS. Or an existing variable to be used as index. The index functions that can be used, but not limited:

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)
  )

# Aggregate to 4-hour interval ---
pedestrian %>% 
  group_by(Sensor) %>% 
# convert to UTC for handling DST in floor_date(), since it does not respect tz
  mutate(Date_Time = lubridate::force_tz(Date_Time, tzone = "UTC")) %>% 
  index_by(Date_Time5 = lubridate::floor_date(Date_Time, "4 hour")) %>%
  summarise(Total_Count = sum(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