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.tables, which should be class compatible for later rbinding. When one of the data.tables is missing a variable, I am creating that column but with it set to all NAs 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:

  1. Why doesn't NA_logical_ exist, when its relatives do?
  2. What should I do about it in the context of data.table or just to avoid coercion as much as possible? I assume using NA_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.
有帮助吗?

解决方案

NA is already logical so NA_logical_ is not needed. Just use NA in those situations where you need a missing logical. Note:

> typeof(NA)
[1] "logical"

Since the NA_*_ names are all reserved words there was likely a desire to minimize the number of them.

Example:

library(data.table)
X <- data.table( a=NA_character_, b=rep(NA,5) )
X[ 3, b:=FALSE ]
> X
    a     b
1: NA    NA
2: NA    NA
3: NA FALSE
4: NA    NA
5: NA    NA

其他提示

I think based on this

 #define NA_LOGICAL R_NaInt

from $R_HOME/R/include/R_ext/Arith.h we can suggest using NA_integer or NA_real and hence plain old NA in R code:

R> as.logical(c(0,1,NA))
[1] FALSE  TRUE    NA
R> 
R> as.logical(c(0L, 1L, NA_integer_))
[1] FALSE  TRUE    NA
R> 

which has

R> class(as.logical(c(0,1,NA)))
[1] "logical"
R> 
R> class(as.logical(c(0, 1, NA_real_)))
[1] "logical"
R> 

Or am I misunderstanding your question? R's logical type is three-values: yay, nay and missing. And we can use the NA from either integer or numeric to cast. Does that help?

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top