如何解决dplyr mutate case_when 产生 NA
我在 case_when()
中使用 mutate()
并获得预期输出,但我也收到有关生成 NA 的警告。不过,我想不通为什么。据我所知,无论如何,RHS 都应该属于同一类型。我已将所有 RHS 条件包装在 as.double()
调用中,以确保它们属于同一类型,但这并没有什么区别。警告暗示我的默认条件是创建 NA。
依赖
我正在使用 library(mc2d)
来获取下面使用的 rpert()
函数。
源数据
> simulationTable %>% select(count,Productivity_Low__c,Productivity_ML__c,Productivity_High__c)
# A tibble: 20 x 4
# Rowwise:
count Productivity_Low__c Productivity_ML__c Productivity_High__c
<int> <dbl> <dbl> <dbl>
1 2 0 0 0
2 2 0 0 0
3 0 0 0 0
4 2 0 0 0
5 6 0 0 0
6 1 0 0 0
7 0 0 0 0
8 0 0 0 0
9 0 0 0 0
10 0 0 0 0
11 0 0 0 0
12 0 0 0 0
13 0 0 0 0
14 0 0 0 0
15 1 0 0 0
16 0 10000 75000 500000
17 0 10000 75000 500000
18 1 10000 75000 500000
19 0 10000 75000 500000
20 0 10000 75000 500000
我的代码
simulationTable <- simulationTable %>%
mutate(
productivity = case_when(count>0 & Productivity_High__c>0 ~ as.double(sum(rpert(count,Productivity_High__c))),TRUE ~ as.double(0))
)
警告
Problem with `mutate()` input `productivity`.
ℹ NAs produced
ℹ Input `productivity` is `case_when(...)`.
ℹ The error occurred in row 1.NAs producedProblem with `mutate()` input `productivity`.
ℹ NAs produced
ℹ Input `productivity` is `case_when(...)`.
ℹ The error occurred in row 2.NAs producedProblem with `mutate()` input `productivity`.
ℹ NAs produced
ℹ Input `productivity` is `case_when(...)`.
ℹ The error occurred in row 4.NAs producedProblem with `mutate()` input `productivity`.
ℹ NAs produced
ℹ Input `productivity` is `case_when(...)`.
ℹ The error occurred in row 5.NAs producedProblem with `mutate()` input `productivity`.
ℹ NAs produced
ℹ Input `productivity` is `case_when(...)`.
ℹ The error occurred in row 6.NAs producedProblem with `mutate()` input `productivity`.
ℹ NAs produced
ℹ Input `productivity` is `case_when(...)`.
ℹ The error occurred in row 15.NAs produced
解决方法
?奇数
和其他人一样,我无法重现这些错误。但是(我不知道这个功能)我没有得到 869414.4 作为答案
temp_id
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。