简体   繁体   English

dplyr 变异 case_when 产生 NA

[英]dplyr mutate case_when producing NA

I'm using case_when() in a mutate() and I'm getting the expected output, but I'm also getting warnings about producing NA.我在mutate() () 中使用了case_when() ,得到了预期的 output,但我也收到了关于产生 NA 的警告。 I can't figure out why, though.不过,我不知道为什么。 From what I can tell, the RHS should be of the same type no matter what.据我所知,无论如何,RHS 应该是同一类型的。 I've wrapped all my RHS conditions in a as.double() call to make sure they are of the same type, but it doesn't make a difference.我已经在as.double()调用中包装了所有 RHS 条件,以确保它们属于同一类型,但这并没有什么不同。 The warnings imply that my default condition is what is creating the NAs.警告意味着我的默认条件是创建 NA 的原因。

dependencies依赖关系

I'm using library(mc2d) to get the rpert() function used below.我正在使用library(mc2d)来获取下面使用的rpert() function。

source data源数据

> 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

my code我的代码

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))
  )

the warnings警告

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

? ? Odd奇怪的

As with others I cant reproduce the errors.与其他人一样,我无法重现错误。 BUT (I don't know the function) I don't get 869414.4 as an answer from但是(我不知道函数)我没有得到 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.2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM