小编Jes*_*err的帖子

Case_when 不满足条件时执行代码

Case_when 即使不满足条件,似乎也执行代码,见下文:

df <- tibble(
    group = c('A', 'A', 'B', 'C'),
    take_max = c(F, F, T, T),
    value = c(NA, NA, 2, 3)
)

df %>%
    group_by(group) %>%
    mutate(
        res = case_when(
            take_max ~ max(value, na.rm = T),
            TRUE ~ 1
            )
    )
    
Run Code Online (Sandbox Code Playgroud)

case_when 正确计算值,但它也会返回此警告:

Warning: Problem with `mutate()` input `res`.
? no non-missing arguments to max; returning -Inf
? Input `res` is `case_when(take_max ~ max(value, na.rm = T), TRUE ~ 1)`.
? The warning occurred in group 1: group …
Run Code Online (Sandbox Code Playgroud)

r dplyr

0
推荐指数
1
解决办法
47
查看次数

标签 统计

dplyr ×1

r ×1