Type
In terms of data type, NA
is logical, NaN
is numeric type and NULL
do not have a specific type. A particularity of NA
is that can be also adapted to the type of vector where is located. In R we cannot have a vector of numbers
and logical
values:
is.logical(NA)
## [1] TRUE
is.logical(NaN)
## [1] FALSE
is.logical(NULL)
## [1] FALSE
vec <- c(1,2,3,4, TRUE, 5, FALSE)
vec # TRUE is 1 and FALSE is 0
## [1] 1 2 3 4 1 5 0
But we can have a numeric, character or logical vector with NA
values and the NA
is forced to the specific type:
# NA alone is logical but
vec <- c(1,2,3,4,NA,5,6)
class(vec)
## [1] "numeric"
for(i in vec) print(class(i))
## [1] "numeric"
## [1] "numeric"
## [1] "numeric"
## [1] "numeric"
## [1] "numeric"
## [1] "numeric"
## [1] "numeric"
# now NA is numeric
This is possible because there are several type of NA
values that are not usually employed directly but are used from R depending on the vector:
is.character(NA_character_)
## [1] TRUE
is.numeric(NA_real_)
## [1] TRUE
is.integer(NA_integer_)
## [1] TRUE
# This
vec <- c(1,2,3,4,5,NA)
vec
## [1] 1 2 3 4 5 NA
# is the same as
vec <- c(1,2,3,4,5,NA_integer_)
vec
## [1] 1 2 3 4 5 NA
The NaN
is a numeric type because is the result of undefined operations:
is.numeric(NaN)
## [1] TRUE
0/0
## [1] NaN
vec <- c(1,2,3,4,NaN)
vec
## [1] 1 2 3 4 NaN
The NULL
value is surely the strangest because represents the absence of any value and the class is NULL
itself meaning that is different from NA
and NaN
values.
x <- NULL
class(x) # the class is NULL
## [1] "NULL"
Compared to NA
and NaN
that are associate with some information (absence of value and undefined number), NULL
values are somehow transparent and cannot be stored in vectors:
vec <- c(1,2,3,4,5, NA, 8, NaN, 11, NULL)
vec # the null value is not present
## [1] 1 2 3 4 5 NA 8 NaN 11
LS0tCnRpdGxlOiAiRGVhbGluZyB3aXRoIE5BL05VTEwvTmFOIgpvdXRwdXQ6IAogICAgYm9va2Rvd246Omh0bWxfZG9jdW1lbnQyOgogICAgICAgIHRvYzogdHJ1ZQogICAgICAgIHRvY19mbG9hdDogdHJ1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