在 R v 4.2.0 中使用 if else 语句抛出错误的条件替换
Conditional replacement using if else statement throwing error in R v 4.2.0
我有一个包含很多重复值的数据集。
数据结构示例:
GRID DATE TIME TAGLFT TAGRT COL LOCX LOCY YEAR TOTAL.sum
<chr> <date> <chr> <chr> <chr> <chr> <dbl> <dbl> <int> <int>
AG 2004-06-01 09:33 47962 47963 P/- 20.5 0 2004 2
AG 2004-06-01 09:33 47962 47963 P/- 20.5 0.5 2004 2
如果 group_by
的 sum
列大于 1,我将尝试有条件地替换 LOCY
值,如下所示:
data %>%
group_by(GRID, DATE, TIME, TAGLFT, TAGRT, COL, LOCX, YEAR) %>%
mutate(LOCY=if(TOTAL.sum>1) first(LOCY) else LOCY)
期望的输出是:
GRID DATE TIME TAGLFT TAGRT COL LOCX LOCY YEAR TOTAL.sum
<chr> <date> <chr> <chr> <chr> <chr> <dbl> <dbl> <int> <int>
AG 2004-06-01 09:33 47962 47963 P/- 20.5 0 2004 2
AG 2004-06-01 09:33 47962 47963 P/- 20.5 0 2004 2
在我将 R 更新到 v 4.2.0 之前,上面的脚本工作正常。
现在,当我 运行 脚本的 mutate 部分时,出现以下错误:
Error in `mutate()`:
! Problem while computing `LOCY = if (sum > 1) first(LOCY) else LOCY`.
ℹ The error occurred in group 73: GRID = "AG", DATE = 2004-06-01, TIME = "09:33", TAGLFT = "47962", TAGRT = "47963", COL = "P/-", LOCX = 20.5, YEAR = 2004.
Caused by error in `if (TOTAL.sum > 1) ...`:
! the condition has length > 1
Run `rlang::last_error()` to see where the error occurred.
知道这里可能发生了什么吗?我的脚本有问题吗?我的语法遗漏了什么吗?
如果需要,这是我的 sessionInfo()
> sessionInfo()
R version 4.2.0 (2022-04-22)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS Mojave 10.14.6
Matrix products: default
BLAS: /Library/Frameworks/R.framework/Versions/4.2/Resources/lib/libRblas.0.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/4.2/Resources/lib/libRlapack.dylib
locale:
[1] en_CA.UTF-8/en_CA.UTF-8/en_CA.UTF-8/C/en_CA.UTF-8/en_CA.UTF-8
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] adehabitatHR_0.4.19 adehabitatLT_0.3.25 CircStats_0.2-6 boot_1.3-28
[5] MASS_7.3-57 adehabitatMA_0.3.14 ade4_1.7-19 deldir_1.0-6
[9] jpeg_0.1-9 ggmap_3.0.0 RgoogleMaps_1.4.5.3 raster_3.5-15
[13] maps_3.4.0 rgdal_1.5-30 rgeos_0.5-9 maptools_1.1-4
[17] sp_1.4-7 data.table_1.14.2 sf_1.0-7 spatsoc_0.1.16
[21] DescTools_0.99.45 glmmTMB_1.1.3 lme4_1.1-29 Matrix_1.4-1
[25] ggnetwork_0.5.10 scales_1.2.0 cowplot_1.1.1 gridExtra_2.3
[29] psych_2.2.5 beepr_1.3 here_1.0.1 magrittr_2.0.3
[33] rcompanion_2.4.15 devtools_2.4.3 usethis_2.1.6 forcats_0.5.1
[37] stringr_1.4.0 purrr_0.3.4 readr_2.1.2 tibble_3.1.7
[41] ggplot2_3.3.6 tidyverse_1.3.1 lubridate_1.8.0 tidyr_1.2.0
[45] plyr_1.8.7 krsp_0.0.2 dplyr_1.0.9
loaded via a namespace (and not attached):
[1] readxl_1.4.0 backports_1.4.1 TMB_1.8.1 splines_4.2.0
[5] TH.data_1.1-1 fansi_1.0.3 memoise_2.0.1 tzdb_0.3.0
[9] remotes_2.4.2 modelr_0.1.8 matrixStats_0.62.0 sandwich_3.0-1
[13] prettyunits_1.1.1 colorspace_2.0-3 rvest_1.0.2 haven_2.5.0
[17] callr_3.7.0 crayon_1.5.1 jsonlite_1.8.0 libcoin_1.0-9
[21] Exact_3.1 keyring_1.3.0 survival_3.3-1 zoo_1.8-10
[25] glue_1.6.2 gtable_0.3.0 emmeans_1.7.4-1 pkgbuild_1.3.1
[29] mvtnorm_1.1-3 DBI_1.1.2 Rcpp_1.0.8.3 xtable_1.8-4
[33] tmvnsim_1.0-2 units_0.8-0 foreign_0.8-82 proxy_0.4-26
[37] stats4_4.2.0 httr_1.4.3 modeltools_0.2-23 ellipsis_0.3.2
[41] pkgconfig_2.0.3 multcompView_0.1-8 dbplyr_2.1.1 utf8_1.2.2
[45] tidyselect_1.1.2 rlang_1.0.2 munsell_0.5.0 cellranger_1.1.0
[49] tools_4.2.0 cachem_1.0.6 cli_3.3.0 generics_0.1.2
[53] audio_0.1-10 broom_0.8.0 fastmap_1.1.0 processx_3.5.3
[57] fs_1.5.2 coin_1.4-2 rootSolve_1.8.2.3 nlme_3.1-157
[61] xml2_1.3.3 brio_1.1.3 compiler_4.2.0 rstudioapi_0.13
[65] png_0.1-7 e1071_1.7-9 testthat_3.1.4 reprex_2.0.1
[69] stringi_1.7.6 ps_1.7.0 desc_1.4.1 lattice_0.20-45
[73] classInt_0.4-3 nloptr_2.0.2 vctrs_0.4.1 pillar_1.7.0
[77] lifecycle_1.0.1 lmtest_0.9-40 estimability_1.3 bitops_1.0-7
[81] lmom_2.8 R6_2.5.1 RMySQL_0.10.23 KernSmooth_2.23-20
[85] gld_2.6.4 sessioninfo_1.2.2 codetools_0.2-18 assertthat_0.2.1
[89] pkgload_1.2.4 rjson_0.2.21 rprojroot_2.0.3 withr_2.5.0
[93] nortest_1.0-4 mnormt_2.0.2 multcomp_1.4-19 expm_0.999-6
[97] parallel_4.2.0 hms_1.1.1 terra_1.5-21 grid_4.2.0
[101] coda_0.19-4 class_7.3-20 minqa_1.2.4 numDeriv_2016.8-1.1
您可以使用 if
的向量化版本,即 ifelse
函数,如下所示:
data %>%
group_by(GRID, DATE, TIME, TAGLFT, TAGRT, COL, LOCX, YEAR) %>%
mutate(LOCY = ifelse(sum > 1, first(LOCY) , LOCY))
#> # A tibble: 2 × 10
# Groups: GRID, DATE, TIME, TAGLFT, TAGRT, COL, LOCX, YEAR [1]
GRID DATE TIME TAGLFT TAGRT COL LOCX LOCY YEAR sum
<chr> <chr> <chr> <int> <int> <chr> <dbl> <dbl> <int> <int>
1 AG 2004-06-01 09:33 47962 47963 P/- 20.5 0 2004 2
2 AG 2004-06-01 09:33 47962 47963 P/- 20.5 0 2004 2
由 reprex package (v2.0.1)
于 2022-05-27 创建
我有一个包含很多重复值的数据集。
数据结构示例:
GRID DATE TIME TAGLFT TAGRT COL LOCX LOCY YEAR TOTAL.sum
<chr> <date> <chr> <chr> <chr> <chr> <dbl> <dbl> <int> <int>
AG 2004-06-01 09:33 47962 47963 P/- 20.5 0 2004 2
AG 2004-06-01 09:33 47962 47963 P/- 20.5 0.5 2004 2
如果 group_by
的 sum
列大于 1,我将尝试有条件地替换 LOCY
值,如下所示:
data %>%
group_by(GRID, DATE, TIME, TAGLFT, TAGRT, COL, LOCX, YEAR) %>%
mutate(LOCY=if(TOTAL.sum>1) first(LOCY) else LOCY)
期望的输出是:
GRID DATE TIME TAGLFT TAGRT COL LOCX LOCY YEAR TOTAL.sum
<chr> <date> <chr> <chr> <chr> <chr> <dbl> <dbl> <int> <int>
AG 2004-06-01 09:33 47962 47963 P/- 20.5 0 2004 2
AG 2004-06-01 09:33 47962 47963 P/- 20.5 0 2004 2
在我将 R 更新到 v 4.2.0 之前,上面的脚本工作正常。
现在,当我 运行 脚本的 mutate 部分时,出现以下错误:
Error in `mutate()`:
! Problem while computing `LOCY = if (sum > 1) first(LOCY) else LOCY`.
ℹ The error occurred in group 73: GRID = "AG", DATE = 2004-06-01, TIME = "09:33", TAGLFT = "47962", TAGRT = "47963", COL = "P/-", LOCX = 20.5, YEAR = 2004.
Caused by error in `if (TOTAL.sum > 1) ...`:
! the condition has length > 1
Run `rlang::last_error()` to see where the error occurred.
知道这里可能发生了什么吗?我的脚本有问题吗?我的语法遗漏了什么吗?
如果需要,这是我的 sessionInfo()
> sessionInfo()
R version 4.2.0 (2022-04-22)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS Mojave 10.14.6
Matrix products: default
BLAS: /Library/Frameworks/R.framework/Versions/4.2/Resources/lib/libRblas.0.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/4.2/Resources/lib/libRlapack.dylib
locale:
[1] en_CA.UTF-8/en_CA.UTF-8/en_CA.UTF-8/C/en_CA.UTF-8/en_CA.UTF-8
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] adehabitatHR_0.4.19 adehabitatLT_0.3.25 CircStats_0.2-6 boot_1.3-28
[5] MASS_7.3-57 adehabitatMA_0.3.14 ade4_1.7-19 deldir_1.0-6
[9] jpeg_0.1-9 ggmap_3.0.0 RgoogleMaps_1.4.5.3 raster_3.5-15
[13] maps_3.4.0 rgdal_1.5-30 rgeos_0.5-9 maptools_1.1-4
[17] sp_1.4-7 data.table_1.14.2 sf_1.0-7 spatsoc_0.1.16
[21] DescTools_0.99.45 glmmTMB_1.1.3 lme4_1.1-29 Matrix_1.4-1
[25] ggnetwork_0.5.10 scales_1.2.0 cowplot_1.1.1 gridExtra_2.3
[29] psych_2.2.5 beepr_1.3 here_1.0.1 magrittr_2.0.3
[33] rcompanion_2.4.15 devtools_2.4.3 usethis_2.1.6 forcats_0.5.1
[37] stringr_1.4.0 purrr_0.3.4 readr_2.1.2 tibble_3.1.7
[41] ggplot2_3.3.6 tidyverse_1.3.1 lubridate_1.8.0 tidyr_1.2.0
[45] plyr_1.8.7 krsp_0.0.2 dplyr_1.0.9
loaded via a namespace (and not attached):
[1] readxl_1.4.0 backports_1.4.1 TMB_1.8.1 splines_4.2.0
[5] TH.data_1.1-1 fansi_1.0.3 memoise_2.0.1 tzdb_0.3.0
[9] remotes_2.4.2 modelr_0.1.8 matrixStats_0.62.0 sandwich_3.0-1
[13] prettyunits_1.1.1 colorspace_2.0-3 rvest_1.0.2 haven_2.5.0
[17] callr_3.7.0 crayon_1.5.1 jsonlite_1.8.0 libcoin_1.0-9
[21] Exact_3.1 keyring_1.3.0 survival_3.3-1 zoo_1.8-10
[25] glue_1.6.2 gtable_0.3.0 emmeans_1.7.4-1 pkgbuild_1.3.1
[29] mvtnorm_1.1-3 DBI_1.1.2 Rcpp_1.0.8.3 xtable_1.8-4
[33] tmvnsim_1.0-2 units_0.8-0 foreign_0.8-82 proxy_0.4-26
[37] stats4_4.2.0 httr_1.4.3 modeltools_0.2-23 ellipsis_0.3.2
[41] pkgconfig_2.0.3 multcompView_0.1-8 dbplyr_2.1.1 utf8_1.2.2
[45] tidyselect_1.1.2 rlang_1.0.2 munsell_0.5.0 cellranger_1.1.0
[49] tools_4.2.0 cachem_1.0.6 cli_3.3.0 generics_0.1.2
[53] audio_0.1-10 broom_0.8.0 fastmap_1.1.0 processx_3.5.3
[57] fs_1.5.2 coin_1.4-2 rootSolve_1.8.2.3 nlme_3.1-157
[61] xml2_1.3.3 brio_1.1.3 compiler_4.2.0 rstudioapi_0.13
[65] png_0.1-7 e1071_1.7-9 testthat_3.1.4 reprex_2.0.1
[69] stringi_1.7.6 ps_1.7.0 desc_1.4.1 lattice_0.20-45
[73] classInt_0.4-3 nloptr_2.0.2 vctrs_0.4.1 pillar_1.7.0
[77] lifecycle_1.0.1 lmtest_0.9-40 estimability_1.3 bitops_1.0-7
[81] lmom_2.8 R6_2.5.1 RMySQL_0.10.23 KernSmooth_2.23-20
[85] gld_2.6.4 sessioninfo_1.2.2 codetools_0.2-18 assertthat_0.2.1
[89] pkgload_1.2.4 rjson_0.2.21 rprojroot_2.0.3 withr_2.5.0
[93] nortest_1.0-4 mnormt_2.0.2 multcomp_1.4-19 expm_0.999-6
[97] parallel_4.2.0 hms_1.1.1 terra_1.5-21 grid_4.2.0
[101] coda_0.19-4 class_7.3-20 minqa_1.2.4 numDeriv_2016.8-1.1
您可以使用 if
的向量化版本,即 ifelse
函数,如下所示:
data %>%
group_by(GRID, DATE, TIME, TAGLFT, TAGRT, COL, LOCX, YEAR) %>%
mutate(LOCY = ifelse(sum > 1, first(LOCY) , LOCY))
#> # A tibble: 2 × 10
# Groups: GRID, DATE, TIME, TAGLFT, TAGRT, COL, LOCX, YEAR [1]
GRID DATE TIME TAGLFT TAGRT COL LOCX LOCY YEAR sum
<chr> <chr> <chr> <int> <int> <chr> <dbl> <dbl> <int> <int>
1 AG 2004-06-01 09:33 47962 47963 P/- 20.5 0 2004 2
2 AG 2004-06-01 09:33 47962 47963 P/- 20.5 0 2004 2
由 reprex package (v2.0.1)
于 2022-05-27 创建