Unable to aggregate by index_by function

34 Views Asked by At

Here are the examples that are listed in the index_by function's help page. When i run it in R studio i get the following results

# Monthly counts across sensors
library(dplyr, warn.conflicts = FALSE)
monthly_ped <- pedestrian %>%
  group_by_key() %>%
  index_by(Year_Month = ~ yearmonth(.)) %>%
  summarise(
    Max_Count = max(Count),
    Min_Count = min(Count)
  )
monthly_ped
  Max_Count Min_Count
1     11273         0

index(monthly_ped) Error in not_tsibble(): ! x is not a tsibble.

Using existing variable

pedestrian %>%
  group_by_key() %>%
  index_by(Date) %>%
  summarise(
    Max_Count = max(Count),
    Min_Count = min(Count)
  )

  Max_Count Min_Count
1     11273         0

Attempt to aggregate to 4-hour interval, with the effects of DST

pedestrian %>%
  group_by_key() %>%
  index_by(Date_Time4 = ~ lubridate::floor_date(., "4 hour")) %>%
  summarise(Total_Count = sum(Count))

  Total_Count
1    45483871
library(lubridate, warn.conflicts = FALSE)

Annual trips by Region and State

tourism %>%
  index_by(Year = ~ year(.)) %>%
  group_by(Region, State) %>%
  summarise(Total = sum(Trips))

    Total
1 1724202

As can be seen all of the tsibbles are not getting aggregated to the desired granularity for the dataset which is completely different from the output the documentation provides for these examples. For referecnce this help page shows what should be happening when using the function https://tsibble.tidyverts.org/reference/index-by.html
Any help is appreciated Thank you

Here is my session info :-

R version 4.3.1 (2023-06-16 ucrt)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 11 x64 (build 22621)

Matrix products: default


locale:
[1] LC_COLLATE=English_India.utf8  LC_CTYPE=English_India.utf8    LC_MONETARY=English_India.utf8 LC_NUMERIC=C                  
[5] LC_TIME=English_India.utf8    

time zone: Asia/Calcutta
tzcode source: internal

attached base packages:
[1] grid      stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] fasster_0.1.0.9100 ggbiplot_0.55      scales_1.2.1       plyr_1.8.8         ggfortify_0.4.16   broom_1.0.5        corrr_0.4.4       
 [8] ggmatplot_0.1.2    fable_0.3.3        feasts_0.3.1       fabletools_0.3.3   tsibbledata_0.4.1  tsibble_1.1.3      fpp3_0.5          
[15] hms_1.1.3          patchwork_1.1.3    stringdist_0.9.10  YaleToolkit_4.2.3  lubridate_1.9.2    forcats_1.0.0      stringr_1.5.0     
[22] dplyr_1.1.2        purrr_1.0.2        readr_2.1.4        tidyr_1.3.0        tibble_3.2.1       ggplot2_3.4.3      tidyverse_2.0.0   

loaded via a namespace (and not attached):
 [1] gtable_0.3.3         dlm_1.1-6            anytime_0.3.9        xfun_0.40            tzdb_0.4.0           vctrs_0.6.3         
 [7] tools_4.3.1          generics_0.1.3       parallel_4.3.1       fansi_1.0.4          highr_0.10           pkgconfig_2.0.3     
[13] distributional_0.3.2 lifecycle_1.0.3      compiler_4.3.1       farver_2.1.1         munsell_0.5.0        codetools_0.2-19    
[19] pillar_1.9.0         crayon_1.5.2         ellipsis_0.3.2       iterators_1.0.14     foreach_1.5.2        tidyselect_1.2.0    
[25] stringi_1.7.12       labeling_0.4.2       colorspace_2.1-0     cli_3.6.1            magrittr_2.0.3       utf8_1.2.3          
[31] withr_2.5.0          backports_1.4.1      rappdirs_0.3.3       timechange_0.2.0     gridExtra_2.3        evaluate_0.21       
[37] knitr_1.43           rlang_1.1.1          Rcpp_1.0.11          glue_1.6.2           rstudioapi_0.15.0    R6_2.5.1            
1

There are 1 best solutions below

0
Rishav Dhariwal On

I was able to resolve the problem. It was due to a package ggbiplot which loaded the plyr package. This messed with the summarise function.