r - 从字符串中计算 NA_integer_

标签 r integer na

如何(从文件中)读取“NA_integer”之类的值,并让 R 将其解释为显式键入 NA_integer

这是我失败的两次尝试:

q <- "NA_integer_"
get(q)        # Returns "Error in get(q) : object 'NA_integer_' not found"
eval(q)       # Returns the character value

这是我能得到的最接近的结果。但我不喜欢它,因为它牺牲了很多普遍性。

f <- "as.integer"
v <- NA
do.call(f, list(v))

编辑:添加尾部下划线

最佳答案

根据?NA

NA is a logical constant of length 1 which contains a missing value indicator. NA can be coerced to any other vector type except raw. There are also constants NA_integer_, NA_real_, NA_complex_ and NA_character_ of the other atomic vector types which support missing values: all of these are reserved words in the R language.

所以我们需要

q <- "NA_integer_"

然后使用

eval(parse(text=q))
#[1] NA

关于r - 从字符串中计算 NA_integer_,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47058499/

相关文章:

c++ - 为什么我的 LP 只能得到整数解?

r - 创建包含缺失值的行位置的数据框

r - 用 R 插入多个 NA 值

r - 如何在表中包含 NA 数据

r - 如何在 Shiny R 中动态更改传单 map 的大小?

swift - 是否可以在 Swift 中区分 Bool 和 Int?

python - 从 pandas 数据框中选择行,其中指定列不全为 NaN

r - dplyr group_by - 混合带或不带引号的变量名称

R 中的 R-CNN 用于分析航空照片

python - 如何在 Python 中将二维数组的字符串值转换为整数值?