我正在使用 dplyr 重新编码。将值重新编码为 NA 时出现错误,但不是 NaN。这是一个例子:
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
最佳答案
运行代码时出现此错误
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
期待双倍
class(NA)
# [1] "logical"
你可以使用 NA_real_
代替,因为那是双倍
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
关于为什么它需要一个 double ,请参阅 ?recode
All replacements must be the same type, and must have either length one or the same length as .x.
我认为这是出乎意料的原因是因为像 c
这样的基本函数不关心元素是否属于同一类型,并且无论如何都会向上转换。所以这行得通:
c(1, NA, 3)
因为对于c
函数:
The output type is determined from the highest type of the components in the hierarchy NULL < raw < logical < integer < double < complex < character < list < expression
关于r - 为什么 dplyr recode 在重新编码为 NA 而不是 NaN 时会产生错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57512107/