concept NULL in category R

appears as: NULL
Exploring Data with R

This is an excerpt from Manning's book Exploring Data with R.

  • A function’s arguments can have default values, these are set in the signature with the = sign; using NULL as a default value is useful in the context of control flow decision points (e.g., using if (!is.null(arg)) { ... })
  • Looks almost identical to the output from Listing 9.2 except that the names that would normally appear before the leftmost colons are not there! Certainly, it would be nicer to have numbers there but instead we need to count through these top-level elements ourselves (easy for small lists, increasingly more difficult for those larger ones). There really aren’t any names and, indeed, using names(b_list) gives us NULL.

    If we don’t want a certain component of the list, assign NULL to it. It’s like sending that component into an R oblivion. Let’s get rid of the tibble in a_list with this type of assignment (Listing 9.11).

    Listing 9.11. Removing the second element of the a_list object using NULL
    ```{r removing_element_with_NULL, paged.print=FALSE}
    a_list$b <- NULL
     
    a_list
    ```
    #> $a
    #>  [1] 1 2 3 4 5 8 7 6 5 4 3 2
    #>
    #> $c
    #> $c$A
    #> [1] 1 2 3
    #>
    #> $c$B
    #> [1] "T" "U" "V" "W" "X" "Y" "Z"

    Yes, the second element has been removed. We could very well use numbers as well to perform the removal with NULL. Let’s try that on a_list in Listing 9.12.

    Listing 9.12. Removing the first element of the a_list object using NULL (this time with an index value)
    ```{r removing_element_with_NULL, paged.print=FALSE}
    a_list$b <- NULL
     
    a_list
    ```
    Practical Data Science with R, Second Edition

    This is an excerpt from Manning's book Practical Data Science with R, Second Edition.

    NULL and NANA (not available) values

    In R NULL is just a synonym for the empty or length-zero vector formed by using the concatenate operator c() with no arguments. For example, when we type c() into the R console, we will see the value NULL returned. In R NULL is not any sort of invalid pointer (as it is in most C/Java-related languages). NULL is simply a length-zero vector. Concatenating NULL is a safe and well-defined operation (in fact it’s a “no operation” or “no-op” that does nothing). For example, c(c(), 1, NULL) is perfectly valid and returns the value 1.

    NULL and NANA (not available) values

    R has two special values: NULL and NA. In R, NULL is just an alias for c(), the empty vector. It carries no type information, so an empty vector of numbers is the same type as an empty vector of strings (a design flaw, but consistent with how most programming languages handle so-called null pointers). NULL can only occur where a vector or list is expected; it can’t represent missing scalar values (like a single number or string).

    Beyond Spreadsheets with R

    This is an excerpt from Manning's book Beyond Spreadsheets with R.

    NULL represents part of a structure that’s not just missing but nonexistent entirely; the underlying structure of a NULL object is inherently different.6  NA and NULL differ in that NA represents a value that could have existed but doesn’t, whereas NULL represents the lack of even the structure in which the value could have existed. NaN (not a number) refers to values that are undefined (such as 0/0), though these are rarely encountered.

    6  Technically, there is only a singleNULL object in any R session, and any instances of variables given this value point to that.

    If you try to create an empty vector, R treats this as NULL because it doesn’t have a type to infer:

    The empty value NULL, though, cannot appear in a vector:

    c(3L, NULL, 5L)
    #> [1] 3 5