【问题标题】:dplyr mutate case_when producing NAdplyr mutate case_when 产生 NA
【发布时间】: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(...?我不明白。
  • 我尝试了您的示例,但没有给出该错误。这就是我建议给显示错误的小数据结构的原因

标签: r dplyr case-when


【解决方案1】:

?奇数

与其他人一样,我无法重现错误。但是(我不知道函数)我没有得到 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

【讨论】:

  • rpert 生成随机数。所以我不希望你得到相同的输出。
  • 我的 tibble 一定有什么奇怪的地方,但我不知道如何解决这个问题。
  • 我在您的错误消息中看到的是行数 > 0:1、2、4、5、6、15 和 18。但 18 没有错误。第 18 行的生产力列中没有 0。其他人都这样做。
猜你喜欢
  • 1970-01-01
  • 2022-11-09
  • 2020-02-24
  • 1970-01-01
  • 1970-01-01
  • 2018-05-28
  • 1970-01-01
  • 2016-01-10
  • 1970-01-01
相关资源
最近更新 更多