From help("NA")
:
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.
My question is why there is no NA_logical_
or similar, and what to do about it.
Specifically, I am creating several large very similar data.table
s, which should be class compatible for later rbind
ing. When one of the data.table
s is missing a variable, I am creating that column but with it set to all NA
s of the particular type. However, for a logical I can't do that.
In this case, it probably doesn't matter too much (data.table
dislikes coercing columns from one type to another, but it also dislikes adding rows, so I have to create a new table to hold the rbound version anyway), but I'm puzzled as to why the NA_logical_
, which logically should exist, does not.
Example:
library(data.table)
Y <- data.table( a=NA_character_, b=rep(NA_integer_,5) )
Y[ 3, b:=FALSE ]
Y[ 2, a:="zebra" ]
> Y
a b
1: NA NA
2: zebra NA
3: NA 0
4: NA NA
5: NA NA
> class(Y$b)
[1] "integer"
Two questions:
- Why doesn't
NA_logical_
exist, when its relatives do? - What should I do about it in the context of
data.table
or just to avoid coercion as much as possible? I assume usingNA_integer_
buys me little in terms of coercion (it will coerce the logical I'm adding in to 0L/1L, which isn't terrible, but isn't ideal.