r - 为什么 dplyr recode 在重新编码为 NA 而不是 NaN 时会产生错误

标签 r dplyr

我正在使用 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/

相关文章:

r - "tidyr like"从不同的列中填充 na

r - 将 R 包安装到特定目录

r - 垂直组合多个矩阵

r - 基于 R 中的字符串模式更新字符变量

r - 具有附加条件的唯一唯一变量的累积和

r - 将数据帧的每一行乘以它的向量 R

r - 从 R 中的数据框中获取净值作为比例

r - 你如何计算从 Shiny 中 textInput 框中获取的数据?

r - 在 R 中下载 URL 不以文件扩展名结尾的文件

r - 从有条件的组中选择一行?