You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用index_by函数无法按索引聚合,tsibble未达预期粒度求助

tsibble包index_by函数聚合异常问题

月度传感器流量统计

# 各传感器月度流量统计
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.

使用现有变量聚合

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

运行结果:

Max_Count Min_Count
1     11273         0

4小时间隔聚合(考虑夏令时影响)

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

运行结果:

Total_Count
1    45483871

按地区和州统计年度出行次数

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

运行结果:

Total
1 1724202

所有操作生成的结果均未按数据集期望粒度进行聚合,与index_by函数官方文档示例输出完全不符。

会话信息

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            

内容的提问来源于stack exchange,提问作者Rishav Dhariwal

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.09 11:39:52