首页 > 解决方案 > 如何解决 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))

标签: r

解决方案


根据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 创建


推荐阅读