x <- c(1, 2, NA, NULL, 5) length(x)
In R, NULL values are dropped when combined into vectors, so x contains 4 elements: 1, 2, NA, and 5. The length() function returns 4.
a <- NA b <- NULL is.na(a) is.na(b) is.null(a) is.null(b)
is.na(NA) returns TRUE because NA is a missing value.is.na(NULL) returns a logical vector of length 0 because NULL has no elements.is.null(NA) is FALSE because NA is not NULL.is.null(NULL) is TRUE.
x <- c(1, 2, NA, 4) if (x == NA) { print("Has NA") } else { print("No NA") }
In R, == NA returns NA, not TRUE or FALSE. The if statement requires a TRUE or FALSE condition, so it fails to detect NA this way. Use is.na() to check for NA values.
NULL means no value or no object at all, often used to indicate absence.NA means a missing value inside an object like a vector or data frame.
lst <- list(a = 1, b = NA, c = NULL, d = 4) sum(unlist(sapply(lst, is.na))) length(lst)
The list lst has 4 elements; NULL is kept unlike in vectors.sapply(lst, is.na) returns list(FALSE, TRUE, logical(0), FALSE); unlist() drops the empty vector, so sum(c(FALSE, TRUE, FALSE)) = 1.
Length of lst is 4.