R 因子列不运行时的情况
这似乎很公平,可能是一个错误,或者我错过了一些非常基本的东西。我尝试将物种转换为二进制变量&因此,对于一个简单的操作,使用case,但是收到一个错误,不确定是否应该出现R 因子列不运行时的情况,r,dplyr,tidyverse,R,Dplyr,Tidyverse,这似乎很公平,可能是一个错误,或者我错过了一些非常基本的东西。我尝试将物种转换为二进制变量&因此,对于一个简单的操作,使用case,但是收到一个错误,不确定是否应该出现 iris %>% dplyr::mutate(Species=as.factor(Species), Species=case_when(Species=="setosa"~"virginica", TRUE~Spec
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)`.
有关会话信息的详细信息
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
iris
数据集已默认按因子设置Species
列。您希望在此处输入字符类型,因此:
iris%>%
dplyr::突变(物种=作为特征(物种),
物种=案例(物种==“setosa”~“virginica”,真~物种)
在因子变量上使用case\u有点棘手
case\u当
为类型严格时,意味着所有值应计算为同一类型。您拥有的第一个值是character(“virginica”)类型,而TRUE
值是factor
类型,因此会出现类型不匹配错误。此外,所有值应具有与原始数据相同级别的因子。因此,结合所有这些变化,您可以:
library(dplyr)
iris %>%
mutate(Species=case_when(Species == "setosa" ~
factor("virginica", levels = unique(.$Species)),
TRUE ~ Species))
但是我不能在因子类型上使用case\u吗?如果在引擎盖下case\u时
将使用=
进行相等比较,那么可能不行。然后你需要做一些类似于比较水平的事情,而不是实际的因子变量本身。为了简单起见,这里最好只使用字符
。