【问题标题】:R case_when with multiple conditionsR case_when 有多个条件
【发布时间】:2020-03-10 08:06:44
【问题描述】:

这是我要运行的函数。

fx_SegmentCustomer <- function(Rx,Fx,Mx){
  case_when(between(Rx,4,5) , between(Fx,4,5), between(Mx,4,5) ~ "xyz",
            TRUE ~ NA_character_)
}

fx_SegmentCustomer(5,4,5)

得到以下错误:

块引用

错误:案例 1 (between(Rx, 4, 5)) 必须是双边公式,而不是 逻辑向量

我猜逻辑向量需要转换成布尔值,但是如何?

【问题讨论】:

    标签: r dplyr


    【解决方案1】:

    我猜你的意思是用 AND (&amp;) 分隔条件。

    library(dplyr)
    
    fx_SegmentCustomer <- function(Rx,Fx,Mx){
      case_when(between(Rx,4,5) & between(Fx,4,5) & between(Mx,4,5) ~ "xyz",
                TRUE ~ NA_character_)
    }
    
    fx_SegmentCustomer(5,4,5)
    #[1] "xyz"
    

    【讨论】:

    • 其实应该是“和”。
    • 我的印象是逗号在某些地方可以充当 AND(&)。
    猜你喜欢
    • 2021-09-17
    • 2022-01-02
    • 2021-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-10
    • 1970-01-01
    • 2020-08-21
    相关资源
    最近更新 更多