You need to enable JavaScript to run this app. R版本更新后tidyverse的group_by全量数据分组异常问题
优惠活动
大模型
产品
解决方案
定价
更多

R版本更新后tidyverse的group_by全量数据分组异常问题

问题背景

升级R版本后,tidyverse生态下的group_by函数出现运行异常:此前可正常执行的代码,升级后调用group_by时会将数据集每一行识别为独立分组。
通过对照测试验证:

  • 若先将数据集筛选为仅包含单个(x,y)分组的子集再运行代码,输出结果符合预期
  • 若对全量数据集运行完全相同的代码,输出与预期不符,原本应生成35个分组,实际结果不符合要求

对照测试代码

单分组子集测试(输出符合预期)

dtt_xy <- dtt %>%
  filter(x == -121 & y == 65)
dtt_xy

dtt_output <- dtt_xy %>%
  group_by(x, y) %>%
  group_by(grp = cumsum(c(TRUE, diff(Date) != 1)), .add = TRUE)
dtt_output # 预期输出

全量数据集测试(输出异常)

dtt_output <- dtt %>%
  group_by(x, y) %>%
  group_by(grp = cumsum(c(TRUE, diff(Date) != 1)), .add = TRUE)
dtt_output # 非预期输出,原本应生成35个分组

测试数据集dtt

dtt <- structure(list(x = c(-121, -120, -121, -120, -121, -120, -121, 
-120, -121, -120, -121, -120, -121, -120, -121, -120, -121, -120, 
-121, -120, -121, -121, -121, -120, -121, -120, -121, -120, -121, 
-120, -121, -120, -121, -120, -121, -120, -121, -120, -121, -120, 
-121, -120, -121, -120, -121, -120, -121, -120, -121, -120, -121, 
-120, -121, -120, -121, -120, -121, -120, -121, -120, -121, -120, 
-121, -120, -121, -120, -121, -120, -121, -120, -121, -120, -121, 
-120, -121, -120, -121, -120, -121, -120, -121, -120, -121, -120, 
-121, -120, -120, -120, -121, -120, -121, -120, -121, -120, -121, 
-120, -121, -120, -121, -120, -121, -120, -121, -120, -121, -120, 
-121, -120, -121, -120, -121, -120, -121, -120, -121, -120, -121, 
-120, -121, -120, -121, -120, -121, -120, -121, -120, -121, -120, 
-121, -120, -121, -120, -121, -120, -121, -120, -121, -120, -121, 
-120, -121, -120, -121, -120, -121, -120, -121, -120, -121, -120
), y = c(65, 65, 63, 63, 65, 65, 63, 63, 65, 65, 63, 63, 65, 
65, 63, 63, 65, 65, 63, 63, 65, 63, 65, 65, 63, 63, 65, 65, 63, 
63, 65, 65, 63, 63, 65, 65, 63, 63, 65, 65, 63, 63, 65, 65, 63, 
63, 65, 65, 63, 63, 65, 65, 63, 63, 65, 65, 63, 63, 65, 65, 63, 
63, 65, 65, 63, 63, 65, 65, 63, 63, 65, 65, 65, 65, 65, 65, 65, 
65, 63, 63, 65, 65, 63, 63, 63, 63, 65, 63, 63, 63, 65, 65, 63, 
63, 65, 65, 63, 63, 65, 65, 63, 63, 65, 65, 63, 63, 63, 63, 65, 
65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 63, 63, 65, 65, 63, 63, 
65, 65, 65, 65, 63, 63, 65, 65, 63, 63, 65, 65, 63, 63, 65, 65, 
63, 63, 65, 65, 63, 63, 65, 65, 63, 63), Date = structure(c(5123, 
5123, 5123, 5123, 5124, 5124, 5124, 5124, 5125, 5125, 5125, 5125, 
5126, 5126, 5126, 5126, 5127, 5127, 5127, 5127, 5128, 5128, 5177, 
5177, 5177, 5177, 5178, 5178, 5178, 5178, 5179, 5179, 5179, 5179, 
5180, 5180, 5180, 5180, 5181, 5181, 5181, 5181, 5200, 5200, 5200, 
5200, 5201, 5201, 5201, 5201, 5202, 5202, 5202, 5202, 5203, 5203, 
5203, 5203, 5204, 5204, 5204, 5204, 5205, 5205, 5205, 5205, 5206, 
5206, 5206, 5206, 5238, 5238, 5239, 5239, 5240, 5240, 5273, 5273, 
5273, 5273, 5274, 5274, 5274, 5274, 5319, 5319, 5320, 5325, 5326, 
5326, 5327, 5327, 5327, 5327, 5328, 5328, 5328, 5328, 5329, 5329, 
5329, 5329, 5330, 5330, 5330, 5330, 5331, 5331, 5344, 5344, 5345, 
5345, 5381, 5381, 5382, 5382, 5383, 5383, 5383, 5383, 5384, 5384, 
5384, 5384, 5401, 5401, 5402, 5402, 5402, 5402, 5403, 5403, 5403, 
5403, 5404, 5404, 5404, 5404, 5405, 5405, 5405, 5405, 5406, 5406, 
5406, 5406, 5407, 5407, 5407, 5407), class = "Date")), row.names = c(NA, 
-150L), class = c("tbl_df", "tbl", "data.frame"))

R会话运行环境信息

R version 4.0.4 (2021-02-15)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19042)

Matrix products: default

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252   
[3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

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

other attached packages:
[1] forcats_0.5.1   stringr_1.4.0   dplyr_1.0.5     purrr_0.3.4     readr_1.4.0     tidyr_1.1.3    
[7] tibble_3.1.0    ggplot2_3.3.3   tidyverse_1.3.0

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.6       cellranger_1.1.0 pillar_1.5.1     compiler_4.0.4   dbplyr_2.1.0     tools_4.0.4     
 [7] jsonlite_1.7.2   lubridate_1.7.10 lifecycle_1.0.0  gtable_0.3.0     pkgconfig_2.0.3  rlang_0.4.10    
[13] reprex_1.0.0     cli_2.3.1        rstudioapi_0.13  DBI_1.1.1        haven_2.3.1      withr_2.4.1     
[19] xml2_1.3.2       httr_1.4.2       fs_1.5.0         generics_0.1.0   vctrs_0.3.6      hms_1.0.0       
[25] grid_4.0.4       tidyselect_1.1.0 glue_1.4.2       R6_2.5.0         fansi_0.4.2      readxl_1.3.1    
[31] modelr_0.1.8     magrittr_2.0.1   backports_1.2.1  scales_1.1.1     ellipsis_0.3.1   rvest_1.0.0     
[37] assertthat_0.2.1 colorspace_2.0-0 utf8_1.2.1       stringi_1.5.3    munsell_0.5.0    broom_0.7.5     
[43] crayon_1.4.1 

内容的提问来源于stack exchange,提问作者Lily Nature

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.28 17:45:04