为什么dplyr重新编码在重新编码为NA而不是NaN时会产生错误 [英] Why does dplyr recode generate error when recoding to NA but not NaN
问题描述
我正在用dplyr进行编码.将值重新编码为NA而不是NaN时出现错误.这是一个示例:
I'm recoding with dplyr. I'm getting an error when I recode a value to NA, but not NaN. Here's an example:
df <- df %>% mutate(var=recode(var,`2`=0,`3`=NaN))
工作正常,而
df <- df %>% mutate(var=recode(var,`2`=0,`3`=NA))
给我以下错误:
Error: Vector 2 must be a double vector, not a logical vector
推荐答案
运行代码时,您会收到此错误
When running the code you get this error
tibble(var = rep(2:3, 4)) %>%
mutate(var=recode(var,`2`=0,`3`=NA))
# Error: Vector 2 must be a double vector, not a logical vector
这是因为 NA
是合乎逻辑的,但 recode
却希望加倍
This is because NA
is logical, but recode
is expecting a double
class(NA)
# [1] "logical"
您可以改用 NA_real _
,因为那是双精度的
You can use NA_real_
instead, since that's a double
class(NA_real_)
# [1] "numeric"
is.double(NA_real_)
# [1] TRUE
tibble(var = rep(2:3, 4)) %>%
mutate(var=recode(var,`2`=0,`3`=NA_real_))
# var
# <dbl>
# 1 0
# 2 NA
# 3 0
# 4 NA
# 5 0
# 6 NA
# 7 0
# 8 NA
有关为什么期望加倍,请参见?recode
For why it's expecting a double, see ?recode
所有替换必须是相同类型,并且长度必须相同与.x相同或相同的长度.
All replacements must be the same type, and must have either length one or the same length as .x.
我认为这是出乎意料的原因是因为像 c
这样的基本函数并不关心元素的类型是否相同,反而只会向上转换.这样就可以了:
I think the reason this is unexpected is because base functions like c
don't care if the elements are of the same type and will just convert upwards anyway. So this works:
c(1, NA, 3)
由于 c
函数:
根据组件的最高类型确定输出类型在层次结构中,NULL<原始<逻辑上整数<双重<复数<字符<清单<表达
The output type is determined from the highest type of the components in the hierarchy NULL < raw < logical < integer < double < complex < character < list < expression
这篇关于为什么dplyr重新编码在重新编码为NA而不是NaN时会产生错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!