How do I read a value (from a file) like "NA_integer" and have R interpret it as if NA_integer was typed explicitly.
Here are two of my failed attempts:
q <- "NA_integer_"
get(q) # Returns "Error in get(q) : object 'NA_integer_' not found"
eval(q) # Returns the character value
This is the closest I could get. But I don't like it because it sacrifices a lot of generalizability.
f <- "as.integer"
v <- NA
do.call(f, list(v))
edit: added the trailing underscore
According to ?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.
So we need
q <- "NA_integer_"
and then use
eval(parse(text=q))
#[1] NA
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With