## 3.7NULL

To finish up this chapter, I want to talk about one final important data structure that’s closely related to vectors: NULL. NULL is special because it has a unique type, is always length zero, and can’t have any attributes:

typeof(NULL)
#> [1] "NULL"

length(NULL)
#> [1] 0

x <- NULL
attr(x, "y") <- 1
#> Error in attr(x, "y") <- 1: attempt to set an attribute on NULL

You can test for NULLs with is.null():

is.null(NULL)
#> [1] TRUE

There are two common uses of NULL:

• To represent an empty vector (a vector of length zero) of arbitrary type. For example, if you use c() but don’t include any arguments, you get NULL, and concatenating NULL to a vector will leave it unchanged:

c()
#> NULL
• To represent an absent vector. For example, NULL is often used as a default function argument, when the argument is optional but the default value requires some computation (see Section 6.5.3 for more on this). Contrast this with NA which is used to indicate that an element of a vector is absent.

If you’re familiar with SQL, you’ll know about relational NULL and might expect it to be the same as R’s. However, the database NULL is actually equivalent to R’s NA.