我在mutate()中使用了case_when(),并获得了预期的输出,但我也收到了有关生成NA的警告。不过,我不知道为什么。据我所知,RHS无论如何都应该是同一类型的。我已经将我的所有RHS条件包装在一个as.double()调用中,以确保它们属于同一类型,但这没有什么区别。这些警告意味着我的默认条件是创建NAs的原因。
依赖关系
我使用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发布于 2021-02-14 03:36:38
?奇怪
和其他人一样,我不能重现错误。但是(我不知道函数)我得不到869414.4的答案
require(mc2d)
a <- as.integer(1)
b <- as.double(0)
c <- as.double(10000)
d <- as.double(75000)
e <- as.double(500000)
as.double(sum(rpert(a, c, d, e)))
[1] 156468.2https://stackoverflow.com/questions/66188645
复制相似问题