从字符串计算 NA_integer_

Evaluate NA_integer_ from a string

我如何(从文件中)读取一个值,如 "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

也许在读取文件时设置 na.strings 参数,参见下面的示例:

# dummy file
write.csv(data.frame(myCol = c(1:2, "NA", "NA_integer_")),
          "myFile.csv")


# this reads only NA as NA, and column is class of Factor, not what we want...
df1 <- read.csv("myFile.csv")
is.na(df1$myCol)
# [1] FALSE FALSE  TRUE FALSE
str(df1)
# 'data.frame': 4 obs. of  2 variables:
#   $ X    : int  1 2 3 4
#   $ myCol: Factor w/ 3 levels "1","2","NA_integer_": 1 2 NA 3


# once we set na.strings, it reads both NAs and NA_integer_ as NAs and column is class of int.
df1 <- read.csv("myFile.csv", na.strings = c("NA", "NA_integer_"))
is.na(df1$myCol)
# [1] FALSE FALSE  TRUE  TRUE
str(df1)
# 'data.frame': 4 obs. of  2 variables:
#   $ X    : int  1 2 3 4
#   $ myCol: int  1 2 NA NA