r - 如何解决 case when 和 mutate 的问题?
问题描述
我正在尝试根据贷款对列中的变量进行分类。如果贷款已全额支付,则应标记为良好,如果违约或被注销,则应标记为不良。但是,当我在 R 中运行以下代码时,出现此错误:
Error: Problem with `mutate()` input `new_status`. x must be a character vector, not a logical vector. ℹ Input `new_status` is `case_when(...)`.
这是代码块
loans <- loansdf %>% mutate(new_status = case_when(
status %in% c("Fully paid") ~ "Good",
status %in% c("Default", "Charged off") ~ "Bad",
TRUE ~ NA))
解决方案
根据case_when
文档:
# All RHS values need to be of the same type. Inconsistent types will throw an error.
# This applies also to NA values used in RHS: NA is logical, use
# typed values like NA_real_, NA_complex, NA_character_, NA_integer_ as appropriate.
在这种情况下,以及 IMO,我会选择使用逻辑运算符==
而不是与%in%
.
loansdf <- data.frame(
name = c("Eric Fletcher", "Hadley Smith", "Homer Simpson", "Pauline Tator Tots"),
status = c("Fully Paid", "Default", "Charged Off", "Test")
)
library(dplyr)
loansdf %>%
mutate(
new_status = case_when(
status =="Fully Paid" ~ "Good",
status == "Default" | status == "Charged Off" ~ "Bad",
TRUE ~ as.character(NA)
)
)
#> name status new_status
#> 1 Eric Fletcher Fully Paid Good
#> 2 Hadley Smith Default Bad
#> 3 Homer Simpson Charged Off Bad
#> 4 Pauline Tator Tots Test <NA>
由reprex 包(v0.3.0)于 2021-03-09 创建
推荐阅读
- javascript - 如何停止在 MySQL 中舍入 int 值
- kubernetes - kubectl deployment.yaml 权限被拒绝
- javascript - 与 Ajax 一起提交两个 DropZone
- .net - Treeview raising drawnode but some changes won't propagate to the screen
- asp.net-core - Elmah for .NET Core - manually logging
- linux - When send() returns EWOULDBLOCK/EAGAIN, is it possible for it to have written partial bytes?
- docker - Maven 错误未知主机 repo.maven.apache.org
- php - 如何制作一个不区分大小写的部分文本搜索引擎,该引擎将 Regex 与 MongoDB 和 PHP 结合使用?
- algorithm - java中二维数组的最大路径,任意单元开始
- python - 如何从 iFrame 索引切换回相对父级?