Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Evaluate NA_integer_ from a string

Tags:

integer

r

na

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

like image 480
wibeasley Avatar asked Sep 19 '26 09:09

wibeasley


1 Answers

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
like image 85
akrun Avatar answered Sep 21 '26 22:09

akrun



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!