In R programming, NA is a logical constant used denote a missing value. The length of NA is 1.
In this tutorial, we will learn what NA is in R programming, how to assign NA to a variable, and how to check if a value is NA or not.
Assign NA to a Variable
To assign NA to a variable x, use the following syntax.
x <- NA
Check if Value is NA
To check if a value stored in a variable x is NA, use the following expression.
is.na(x)
In the following program, we will assign NA to a variable x. We shall programmatically check if x is having missing value NA or not.
example.R
x <- NA
print(is.na(x))
Output
[1] TRUE
If x is a list, then is.na(x) returns TRUE or FALSE for each item in the list.
In the following program, we will create a list with some values, and programmatically check if these are NA or not.
example.R
x <- list("Hello", NA)
print(is.na(x))
Output
[1] FALSE TRUE
Check if there is any NA
We can check if there is any NA in given atomic vector, list, or pairlist.
In the following program, we will take a list where an element is NA. anyNA must return TRUE, since there is at least one NA in the list.
example.R
x <- list("Hello", NA)
print(anyNA(x))
Output
[1] TRUE
Length of NA
NA has a length of 1. Let us print the length of NA to output using length() function.
example.R
x <- NA
print(length(x))
Output
[1] 1
Conclusion
In this R Tutorial, we learned how to find the minimum value of given arguments using min() function, with the help of examples.
