gpt4 book ai didi

r - 不为因子列运行的情况

转载 作者:行者123 更新时间:2023-12-04 08:28:57 27 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





case_when in mutate pipe

(6 个回答)


9 个月前关闭。




这似乎很公平,也许是一个错误,或者我遗漏了一些非常基本的东西。我尝试将 Species 转换为二进制变量 & 因此使用 case when 进行简单操作,但是收到不确定应该出现的错误。

 iris %>% 
dplyr::mutate(Species=as.factor(Species),
Species=case_when(Species=="setosa"~"virginica",
TRUE~Species))


Error: Problem with `mutate()` input `Species`.
x must be a character vector, not a `factor` object.
i Input `Species` is `case_when(Species == "setosa" ~ "virginica", TRUE ~ Species)`.
session 信息的详细信息
 sessionInfo()
R version 4.0.3 (2020-10-10)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 17763)

Matrix products: default

locale:
[1] LC_COLLATE=English_United States.1252
[2] LC_CTYPE=English_United States.1252
[3] LC_MONETARY=English_United States.1252
[4] 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] conflicted_1.0.4 extrafontdb_1.0 extrafont_0.17 forcats_0.5.0
[5] purrr_0.3.4 readr_1.4.0 tidyr_1.1.2 tibble_3.0.4
[9] tidyverse_1.3.0 ggplot2_3.3.2 dplyr_1.0.2 stringr_1.4.0

loaded via a namespace (and not attached):
[1] qpdf_1.1 xfun_0.19 tidyselect_1.1.0
[4] haven_2.3.1 snakecase_0.11.0 colorspace_1.4-1
[7] vctrs_0.3.4 generics_0.1.0 usethis_1.6.3
[10] htmltools_0.5.0 yaml_2.2.1 utf8_1.1.4
[13] rlang_0.4.8 pillar_1.4.6 glue_1.4.2
[16] withr_2.3.0 DBI_1.1.0 dbplyr_2.0.0
[19] modelr_0.1.8 readxl_1.3.1 lifecycle_0.2.0
[22] munsell_0.5.0 gtable_0.3.0 cellranger_1.1.0
[25] rvest_0.3.6 memoise_1.1.0 evaluate_0.14
[28] knitr_1.30 curl_4.3 fansi_0.4.1
[31] Rttf2pt1_1.3.8 broom_0.7.2 pdftools_2.3.1
[34] Rcpp_1.0.5 scales_1.1.1 backports_1.2.0
[37] jsonlite_1.7.1 fs_1.5.0 hms_0.5.3
[40] askpass_1.1 digest_0.6.27 stringi_1.5.3
[43] grid_4.0.3 cli_2.1.0 tools_4.0.3
[46] magrittr_1.5 crayon_1.3.4 pkgconfig_2.0.3
[49] ellipsis_0.3.1 xml2_1.3.2 reprex_0.3.0
[52] lubridate_1.7.9 tidytuesdayR_1.0.1 assertthat_0.2.1
[55] rmarkdown_2.5 httr_1.4.2 rstudioapi_0.12
[58] R6_2.5.0 compiler_4.0.3

最佳答案

使用 case_when关于因子变量有点棘手。case_when is type strict 意味着所有值都应该评估为相同的类型。您拥有的第一个值是字符类型(“virginica”)和 TRUE值的类型是 factor因此你会在那里得到一个类型不匹配错误。此外,所有值都应具有与原始数据相同水平的因子。因此,您可以合并所有这些更改:

library(dplyr)

iris %>%
mutate(Species=case_when(Species == "setosa" ~
factor("virginica", levels = unique(.$Species)),
TRUE ~ Species))

关于r - 不为因子列运行的情况,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65120378/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com