【发布时间】:2021-02-13 18:43:21
【问题描述】:
我在mutate() 中使用case_when(),我得到了预期的输出,但我也收到了关于产生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_Low__c, Productivity_ML__c, 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
【问题讨论】:
-
@akrun 输入什么?
dput(simulationTable)还是dput(case_when(...?我不明白。 -
我尝试了您的示例,但没有给出该错误。这就是我建议给显示错误的小数据结构的原因