Having learned the special missing value representation
NA in Section 2.10, we will introduce three additional values to represent unexpected results, namely the
Inf. During the process, we will talk about their relationships to
NA as well.
First, let’s take a look at
str(NULL) #> NULL typeof(NULL) #>  "NULL" length(NULL) #>  0
As you can see
NULL only has class
NULL with no values inside, hence the length is 0. It is worth to have comparison with
NA regarding these items.
str(NA) #> logi NA typeof(NA) #>  "logical" length(NA) #>  1
NULL is often returned by expressions and functions whose value is undefined.
a. Undefined field of a list
The first scenario of
NULL is when you try to access an element of a list that is undefined.
<- list(num = 1:3, char = c("a", "b")) my_list $logi my_list#> NULL
Here, the result is
NULL since logi is not a defined field in
b. Remove an element from a list
You can remove an element from a list by assign it the
length(my_list) #>  2 $num <- NULL my_listlength(my_list) #>  1 my_list#> $char #>  "a" "b"
As you can see from the output, the element
num is removed from
my_list, leading to the length of
my_list reduced by 1.
c. Initialize a list of certain length
NULL value is useful to serve as the default initial value when you want to create a list of certain length using the
<- vector(mode = "list", length = 3) my_list my_list#> [] #> NULL #> #> [] #> NULL #> #> [] #> NULL
It is worth mentioning that the
vector() function is also useful to initialize a vector of given mode and length.
vector("numeric", length = 2) ##default is 0 #>  0 0 vector("logical", length = 2) ##default is FALSE #>  FALSE FALSE vector("integer", length = 2) ##default is 0 #>  0 0 vector("character", length = 2) ##default is empty string #>  "" ""
To check if an element is
NULL, you can’t use the logical comparison
== NULL. Instead, you need to use the
<- NULL a == NULL a #> logical(0) is.null(a) #>  TRUE
It is worth explaining the result of
a == NULL is
logical(0), representing a logical vector of length 0. The underlying reason is that
NULL contains no value and is of length 0. As the
== comparison returns a logical type object, hencing leading to a logical vector of length 0.
NULL values when creating a vector
If you create a vector with
NULL values, all
NULL values will be removed if there exists at least one regular values. If all of them are
NULL values, only one of them will be kept. Note that there is fundamentally different from
NA means the value is there, but the exact value is not available to us.
c(NULL, NULL, 1, NULL) #>  1 c(NULL, NULL) #> NULL c(NA, NA) #>  NA NA
NaN, represents Not a Number, usually appears when you divide 0 by 0.
0/0 #>  NaN
Again, it is worth to look at
str(NaN) #> num NaN typeof(NaN) #>  "double" length(NaN) #>  1
As you can see from the results,
NaN is a numeric vector of length 1, with the value
To check if a value is
NaN, you can’t use the
== NaN similar to checking missing values, instead you need to use the function
<- NaN a == NaN ##resulting an NA value a #>  NA is.nan(a) ##the correct way to check if the value is NaN #>  TRUE is.nan(c(NA, 1, NaN)) #>  FALSE FALSE TRUE
The last special we want to introduce in this section is
Inf, representing the value is positive infinity (\(\infty\)), corresponding to a proper mathematical limit. Similarly, we also have negative infinity:
1/0 #>  Inf -2/0 #>  -Inf Inf > 3 #>  TRUE Inf < -1 #>  FALSE Inf + Inf #>  Inf -Inf + 1e10 #>  -Inf 1/0 - 1/0 #it equals 0/0, hencing NaN #>  NaN
Again, it is worth to look at
str(Inf) #> num Inf typeof(Inf) #>  "double" length(Inf) #>  1
As you can see from the results, similar to
Inf is a numeric vector of length 1, but with the value
To check whether a value is finite or infinite, you can use the
is.finite(1/0) #>  FALSE is.infinite(-3/0) #>  TRUE
We would like to summarize the different behaviors of the four special values in R in the following table.
x <- c(NA, NULL, Inf, NaN), answer the following questions.
1. What’s the length of
2. What’s the class and the storage type of
3. What’s the value of
x + 1? Explain the reason for each element in the result.
4. What’s the value of
x == x? Explain the reason for each element in the result.