It used to work fine and than I updted R!. After I updated R, group_by function gives NA when there is more than one group while works fine for a single group.
In sample data dt I expect values and not NA for column event, however it gives me NA when there is more than 1 group. Session info below.
#Sample Data dt
dt<-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, 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")) Code that does not work for group by
dt_donotwork <- dt%>% group_by(x, y) %>% dplyr::group_by(grp = cumsum(c(TRUE, diff(Date) != 1)), .add = TRUE)%>% mutate(event = if (n() >= 5) cur_group_id()[n() >= 5] else NA) dt_donotwork Code that works for single group
dt_sub<-dt%>% filter(x==-120 & y == 65) dt_sub dt_work <- dt_sub%>% group_by(x, y) %>% dplyr::group_by(grp = cumsum(c(TRUE, diff(Date) != 1)), .add = TRUE)%>% mutate(event = if (n() >= 5) cur_group_id()[n() >= 5] else NA) dt_work Session Info
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 https://stackoverflow.com/questions/66773916/r-tidyverse-group-by-gives-na-after-r-update March 24, 2021 at 10:27AM
没有评论:
发表评论