【发布时间】:2018-11-15 10:17:54
【问题描述】:
一长串dplyr管道的末端是
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.
如果我改用这两个中的任何一个,哪个会消失
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 强制 3 到是整数,对吗?
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))
【问题讨论】:
标签: r if-statement dplyr