问题描述
一长串dplyr管道的结尾是
The end of a long string of dplyr pipes is
mutate(n = if_else(FiscalYear == "FY2018" & Candy == "SNICKERS", n - 3, n))
导致此错误
Error in mutate_impl(.data, dots) : Evaluation error: `false` must be type double, not integer.
如果我做这两个中的任何一个,那会消失
Which goes away if I do either of these two instead
mutate(n = ifelse(FiscalYear == "FY2018" & Candy == "SNICKERS", n - 3, n))
mutate(n = if_else(FiscalYear == "FY2018" & Candy == "SNICKERS", n - 3L, n))
我认为最简单的方法是制作一个简单的可重现的游戏,所以我做了您在下面看到的操作,但我再也找不到错误了.知道发生了什么吗?为什么 ifelse
可以工作而 if_else
不能,为什么如果我将 3 改为 3L if_else
可以工作?我理解 L
coerces 3 是一个整数,对吗?
I thought it'd be easiest to make a simple reproducible recreation so I did what you see below, but I can't get the error anymore. Any idea what's going on? Why does ifelse
work where if_else
doesn't, and why does if_else
work if I change 3 to 3L? I understand L
coerces 3 to be an integer, is that correct?
library(tidyverse)
df <- tribble(
~name, ~fruit, ~qty,
"Bob", "apple", 10,
"Bill", "apple", 10
)
# THIS WORKS AGAIN AS IT SHOULD
df %>% mutate(qty = ifelse(name == "Bob" & fruit == "apple", qty / 2, qty))
# BUT IF_ELSE DOESN'T FAIL THIS TIME, WEIRD
df %>% mutate(qty = if_else(name == "Bob" & fruit == "apple", qty / 2, qty))
推荐答案
if_else
from dplyr
是类型稳定的,这意味着它检查true"和"false"条件是相同的类型.如果不是,if_else
会抛出错误.Base R 中的 ifelse
不会这样做.
if_else
from dplyr
is type-stable, meaning that it checks whether the "true" and "false" conditions are the same type. If they aren't, if_else
throws an error. ifelse
in Base R does not do that.
写作时:
mutate(n = if_else(FiscalYear == "FY2018" & Candy == "SNICKERS", n - 3, n))
我假设 n
最初是一个整数类型,所以 "false" 将是整数类型,n-3
将 "true" 强制为双精度,因为 3
是双倍的."true" 和 "false" 是不同的类型,所以 if_else
会抛出一个错误.
I assume n
was originally an integer type, so "false" would be of integer type, n-3
coerces "true" to a double, because 3
is double. "true" and "false" are of different types, so if_else
throws an error.
写作时:
mutate(qty = if_else(name == "Bob" & fruit == "apple", qty / 2, qty))
qty
很可能已经是一个双精度数,所以将双精度数除以 2
(双精度数)仍然会产生双精度数.真"和假"是同一类型.因此没有错误.
qty
is likely already a double, so dividing a double by 2
(a double) still yields a double. "true" and "false" are the same type. Hence no error.
话虽如此,这可以通过以下 typeof
s 轻松检查:
With that being said, this can easily be checked with the following typeof
s:
> typeof(6)
[1] "double"
> typeof(6L)
[1] "integer"
> typeof(6L-3)
[1] "double"
> typeof(6L-3L)
[1] "integer"
> typeof(6/2)
[1] "double"
来自 Base R 的
ifelse
执行隐式强制转换,将所有内容转换为相同类型.这意味着当 "true" 和 "false" 属于不同类型时,它不会抛出错误.这既方便又危险,因为隐式强制转换后可能会出现意想不到的结果.
ifelse
from Base R does implicit coercing, which converts everything to the same type. This means that it doesn't throw an error when "true" and "false" are of different types. This is both more convenient and dangerous as there might be unexpected results after implicit coercing.
我建议将 ifelse
用于一次性/临时程序,而 if_else
用于当您想利用内置单元测试时.
I recommend using ifelse
for one-off/adhoc programs, and if_else
for when you want to take advantage of the built-in unit test.
这篇关于if_else() `false` 必须是双精度类型,而不是整数 - 在 R 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,WP2