如何(从文件中)读取“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/