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

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

# 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
#> # A tsibble: 95 x 4 [1M] #> # Key: Sensor [4] #> Sensor Year_Month Max_Count Min_Count #> <chr> <mth> <dbl> <dbl> #> 1 Birrarung Marr 2015 Jan 5524 1 #> 2 Birrarung Marr 2015 Feb 10121 1 #> 3 Birrarung Marr 2015 Mar 9858 1 #> 4 Birrarung Marr 2015 Apr 7293 1 #> 5 Birrarung Marr 2015 May 5129 1 #> 6 Birrarung Marr 2015 Jun 7556 0 #> 7 Birrarung Marr 2015 Jul 11224 1 #> 8 Birrarung Marr 2015 Aug 5684 0 #> 9 Birrarung Marr 2015 Sep 7757 0 #> 10 Birrarung Marr 2015 Oct 7085 1 #> # … with 85 more rows
index(monthly_ped)
#> Year_Month
# Using existing variable ---- pedestrian %>% group_by(Sensor) %>% index_by(Date) %>% summarise( Max_Count = max(Count), Min_Count = min(Count) )
#> # A tsibble: 2,752 x 4 [1D] #> # Key: Sensor [4] #> Sensor Date Max_Count Min_Count #> <chr> <date> <dbl> <dbl> #> 1 Birrarung Marr 2015-01-01 1630 44 #> 2 Birrarung Marr 2015-01-02 352 1 #> 3 Birrarung Marr 2015-01-03 226 3 #> 4 Birrarung Marr 2015-01-04 852 4 #> 5 Birrarung Marr 2015-01-05 1427 3 #> 6 Birrarung Marr 2015-01-06 937 5 #> 7 Birrarung Marr 2015-01-07 708 4 #> 8 Birrarung Marr 2015-01-08 568 9 #> 9 Birrarung Marr 2015-01-09 1629 5 #> 10 Birrarung Marr 2015-01-10 2439 10 #> # … with 2,742 more rows
# 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))
#> # A tsibble: 16,512 x 3 [4h] <UTC> #> # Key: Sensor [4] #> Sensor Date_Time5 Total_Count #> <chr> <dttm> <int> #> 1 Birrarung Marr 2015-01-01 00:00:00 3287 #> 2 Birrarung Marr 2015-01-01 04:00:00 316 #> 3 Birrarung Marr 2015-01-01 08:00:00 995 #> 4 Birrarung Marr 2015-01-01 12:00:00 2117 #> 5 Birrarung Marr 2015-01-01 16:00:00 1829 #> 6 Birrarung Marr 2015-01-01 20:00:00 406 #> 7 Birrarung Marr 2015-01-02 00:00:00 44 #> 8 Birrarung Marr 2015-01-02 04:00:00 555 #> 9 Birrarung Marr 2015-01-02 08:00:00 1193 #> 10 Birrarung Marr 2015-01-02 12:00:00 822 #> # … with 16,502 more rows
# Annual trips by Region and State ---- tourism %>% index_by(Year = lubridate::year(Quarter)) %>% group_by(Region, State) %>% summarise(Total = sum(Trips))
#> # A tsibble: 1,463 x 4 [1Y] #> # Key: Region, State [77] #> # Groups: Region [77] #> Region State Year Total #> <chr> <chr> <dbl> <dbl> #> 1 Adelaide South Australia 1998 2226. #> 2 Adelaide South Australia 1999 2218. #> 3 Adelaide South Australia 2000 2418. #> 4 Adelaide South Australia 2001 2264. #> 5 Adelaide South Australia 2002 2275. #> 6 Adelaide South Australia 2003 2203. #> 7 Adelaide South Australia 2004 2437. #> 8 Adelaide South Australia 2005 2034. #> 9 Adelaide South Australia 2006 2225. #> 10 Adelaide South Australia 2007 2317. #> # … with 1,453 more rows