다음과 같은 구조의 매우 큰 데이터 세트가 있습니다.
df <-structure(list(date= structure(c(19034, 19034, 19034, 19034, 19034, 19034, 19034, 19034, 19034, 19034),
class= "Date"),type= c("O/N", "O/N", "T/C", "FB", "T/C", "T/C", "FB", "FB", "O/N", "O/N"),
time= structure(c(28796, 32403, 36003, 43203, 46803, 50404, 50408, 54011, 54014, 58815), units= "secs", class= c("hms", "difftime")),
quantity= c(0.003, 0.007, 0.002, 0.001, 0.03, 0.123, 0.017, 0.019, 0.012, 0.01 ),cumvol= c(0.003, 0.01, 0.012, 0.013, 0.043, 0.166, 0.183, 0.202, 0.214, 0.224)),
class= "data.frame", row.names= c(NA, -10L))
특정 타임스탬프에서 유형 및 날짜별로 수량 데이터를 요약하고 해당 날짜의 총 데이터를 제공하는 테이블을 만들려고 합니다.
time_check <-c(13, 16, 18)
summaryt <-df %>%
mutate(time_hr= hour(time), .after= time) %>%
filter(time_hr %in% time_check) %>%
group_by(date, type, time_hr) %>%
summarize(cat_total= sum(quantity)) %>%
group_by(date) %>%
mutate(date_total= sum(cat_total)) %>%
ungroup()
이상하게도 위의 code(및 여기에 게시된 축약된 데이터 세트)를 사용하면 R을 '새롭게' 열 때 제대로 작동합니다. 그러나 이를 기본 데이터 세트(수백만 개의 obs로 구성됨)에 적용하면 간헐적으로 두 가지 오류가 발생합니다.
Error: Must group by variables found in `.data`.
* Column `TradeDate` is not found.
이것은 다음 행으로 인해 발생한 것 같습니다.
summarize(cat_total= sum(quantity))
cat_total만 남기고 날짜(group_by(date) 행에 필요함)를 포함하여 대부분의 열을 삭제하는 것으로 보입니다.
아주 가끔, 아무 것도 변경하지 않고 code를 실행했는데 다음 오류가 발생했습니다.
Error in [[<-.data.frame(*tmp*, col, value= 13L) :
replacement has 1 row, data has 0
이 오류의 간헐성을 고려할 때 매우 이상합니다.
세션 정보는 다음과 같습니다.
R version 4.0.5 (2021-03-31)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 18363)
Matrix products: default
attached base packages:
[1] compiler stats graphics grDevices utils datasets methods base
other attached packages:
[1] forcats_0.5.1 purrr_0.3.4 readr_2.1.1 tibble_3.1.6 tidyverse_1.3.1 plyr_1.8.6 ggrepel_0.9.1
[8] hms_1.1.1 stringi_1.7.6 zoo_1.8-9 TTR_0.24.3 cowplot_1.1.1 kableExtra_1.3.4 knitr_1.37
[15] matrixStats_0.61.0 magick_2.7.3 gridExtra_2.3 ggthemes_4.2.4 directlabels_2021.1.13 bizdays_1.0.8 extrafont_0.17
[22] lubridate_1.8.0 ggplot2_3.3.5 readxl_1.3.1 data.table_1.14.2 stringr_1.4.0 dplyr_1.0.7
[29] tidyr_1.1.4 rmarkdown_2.11 feather_0.3.5 RDCOMClient_0.94-0
loaded via a namespace (and not attached):
[1] httr_1.4.2 jsonlite_1.7.3 viridisLite_0.4.0 modelr_0.1.8 assertthat_0.2.1 cellranger_1.1.0 yaml_2.2.1 Rttf2pt1_1.3.9 pillar_1.6.4
[10] backports_1.4.1 lattice_0.20-45 glue_1.6.1 quadprog_1.5-8 extrafontdb_1.0 digest_0.6.29 rvest_1.0.2 colorspace_2.0-2 htmltools_0.5.2
[19] pkgconfig_2.0.3 broom_0.7.11 haven_2.4.3 scales_1.1.1 webshot_0.5.2 svglite_2.0.0 tzdb_0.2.0 farver_2.1.0 generics_0.1.1
[28] ellipsis_0.3.2 withr_2.4.3 lazyeval_0.2.2 cli_3.1.1 magrittr_2.0.1 crayon_1.4.2 evaluate_0.14 fs_1.5.2 fansi_1.0.2
[37] xts_0.12.1 xml2_1.3.3 tools_4.0.5 lifecycle_1.0.1 plotly_4.10.0 reprex_2.0.1 munsell_0.5.0 systemfonts_1.0.3 rlang_0.4.12
[46] grid_4.0.5 rstudioapi_0.13 htmlwidgets_1.5.4 crosstalk_1.2.0 labeling_0.4.2 gtable_0.3.0 DBI_1.1.2 curl_4.3.2 R6_2.5.1
[55] fastmap_1.1.0 utf8_1.2.2 Rcpp_1.0.8 vctrs_0.3.8 dbplyr_2.1.1 tidyselect_1.1.1 xfun_0.29
Ahh 멋진 det, 그것은 매력을 작동했습니다!
alec222022-02-14 13:10:26- 답변 # 1
det 덕분에 다음을 변경하여 문제를 해결할 수 있었습니다.
summarize(cat_total= sum(quantity))
받는 사람:
dplyr::summarize(cat_total= sum(quantity))
dplyr::summarize를 시도하십시오. 아마도 plyr::summarize가 사용되었을 것입니다.
det2022-02-14 13:05:07