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.

...

Details

  • 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.

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 [1MONTH] #> # 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,755 x 4 [1DAY] #> # 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,745 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 [1YEAR] #> # 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