r - 将 case_when 与 mutate 和函数一起使用时出错 : getting closest number to zero with NA

标签 r dplyr

我正在尝试标记从事故发生到某人获得保险的最接近天数。 0代表当天,负数代表事故前保险,正数代表事故后保险。

数据

df <- data.frame(id=1:4, accident1=c(-1,3,4, NA), accident2=c(100, -2, NA, NA), accident3=c(-3,1,2, NA))
    
> df
  id accident1 accident2 accident3
1  1        -1       100        -3
2  2         3        -2         1
3  3         4        NA         2
4  4        NA        NA        NA

代码:

library(DescTools)
library(dplyr)

df %>%
  rowwise() %>%
  mutate(magic=
           case_when(
             accident1 <0 |accident2<0 |accident3<0 ~ as.numeric(pmax(accident1, accident2, accident3, na.rm=T)),
             accident1 >0 |accident2>0 | accident3>0 ~ as.numeric(pmin(accident1, accident2, accident3, na.rm=T)),
             accident1 > 0 & accident2 > 0 & accident3>0 ~ as.numeric(pmin(accident1, accident2, accident3, na.rm=T)),
             accident1 < 0 & accident2 < 0 & accident3 < 0 ~ as.numeric(pmax(accident1, accident2, accident3, na.rm=T)),
             TRUE ~ NA_real_)) %>%
         rowwise() %>% 
  # not working
  mutate(magic= case_when(
             (accident1 >0 |accident2<0 |accident3<0) & (accident1 >0 |accident2>0 | accident3>0) ~ 
               Closest(as.numeric(unlist(c(accident1, accident2, accident3))), 0, na.rm=T), TRUE~magic))

数据中没有最后一行的结果(所有 NA):

# A tibble: 3 × 5
# Rowwise: 
     id accident1 accident2 accident3 magic
  <int>     <dbl>     <dbl>     <dbl> <dbl>
1     1        -1       100        -3    -1
2     2         3        -2         1     1
3     3         4        NA         2     2

但是,当我在最后一行尝试使用 NA 时:

Error: Problem with `mutate()` column `magic`.
ℹ `magic = case_when(...)`.
ℹ `magic` must be size 1, not 0.
ℹ Did you mean: `magic = list(case_when(...))` ?
ℹ The error occurred in row 4.
Run `rlang::last_error()` to see where the error occurred.
In addition: Warning message:
Problem with `mutate()` column `magic`.
ℹ `magic = case_when(...)`.
ℹ no non-missing arguments to min; returning Inf
ℹ The warning occurred in row 4. 

关于如何让代码在第 4 行使用 NA 运行有什么建议吗?

最佳答案

主要原因是最后一个元素返回numeric(0),因为所有元素都是NA并且我们使用了na.rm = TRUE > 表示最近。我们可以通过索引返回第一个元素来防止这种情况,并将其更改为 NA

library(dplyr)
df %>% 
  mutate(magic=
           case_when(
             accident1 <0 |accident2<0 |accident3<0 ~ as.numeric(pmax(accident1, accident2, accident3, na.rm=T)),
             accident1 >0 |accident2>0 | accident3>0 ~ as.numeric(pmin(accident1, accident2, accident3, na.rm=T)),
             accident1 > 0 & accident2 > 0 & accident3>0 ~ as.numeric(pmin(accident1, accident2, accident3, na.rm=T)),
             accident1 < 0 & accident2 < 0 & accident3 < 0 ~ as.numeric(pmax(accident1, accident2, accident3, na.rm=T)),
             TRUE ~ NA_real_)) %>%
         rowwise() %>%  
  mutate(magic= case_when(
             (accident1 >0 |accident2<0 |accident3<0) & (accident1 >0 |accident2>0 | accident3>0) ~ 
               Closest(as.numeric(unlist(c(accident1, accident2, accident3))), 0, na.rm=TRUE)[1], TRUE~magic))

-输出

# A tibble: 4 × 5
# Rowwise: 
     id accident1 accident2 accident3 magic
  <int>     <dbl>     <dbl>     <dbl> <dbl>
1     1        -1       100        -3    -1
2     2         3        -2         1     1
3     3         4        NA         2     2
4     4        NA        NA        NA    NA

如果我们只在最近上执行此操作,会更容易理解

> apply(df[-1], 1, function(x) Closest(x, 0, na.rm = TRUE))
[[1]]
accident1 
       -1 

[[2]]
accident3 
        1 

[[3]]
accident3 
        2 

[[4]]
named numeric(0)  ####

该解决方案会建立索引,因此 numeric(0) 变为 NA

numeric(0)[1]
[1] NA

关于r - 将 case_when 与 mutate 和函数一起使用时出错 : getting closest number to zero with NA,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69855458/

相关文章:

r - 在 dplyr 中按行计算和正常计算之间切换

r - 如何在 dbplyr 中执行 floor_date()

r - 列线图因子名称错误不在设计中 : model. 类型 x 时间事件 x.df lambda pred.at

r - R 中的矢量回收概念

r - blogdown::serve_site,复制静态文件时出错

r - pmin() 与 dplyr 在 R

r - 使用 purrr 映射 dplyr::select

r - 使用某些方法缩短向量

r - 了解两个 aggregate() 语法如何处理包含 NA 值的数据框

r - R(dplyr)中的Left Join-观察太多?