NA stands for “Not Available and represents a missing value in R“. You can use functions like is.na(), na.omit(), na.exclude(), or na.fail() to check or handle missing values.
Example 1: Use NA in vector to fill the missing values
Let’s define a vector with an NA value and use the is.na() function to check which component has an NA value; in that case, it returns TRUE as a logical vector; other values will be FALSE.
rv <- c(11, NA, 18, 19, 21)
rv
is.na(rv)
Output
[1] 11 NA 18 19 21
[1] FALSE TRUE FALSE FALSE FALSE
As you can see, our second vector component contains an NA value.
To find the missing values in R, use the is.na() method, which returns the logical vector with TRUE. In our example, is.na() method returns TRUE to that second component, and all the others are FALSE.
Example 2: Using NA in Matrix to fill the missing values.
Let’s fill the empty values of the matrix with NA values and see the output.
rv <- c(11, NA, 18, 19, 21, 46, NA, 29, 20)
mtrx <- matrix(rv, nrow = 3, ncol = 3)
mtrx
Output
[,1] [,2] [,3]
[1,] 11 19 NA
[2,] NA 21 29
[3,] 18 46 20
To check the NA values in Matrix, use the is.na() function.
rv <- c(11, NA, 18, 19, 21, 46, NA, 29, 20)
mtrx <- matrix(rv, nrow = 3, ncol = 3)
is.na(mtrx)
Output
[,1] [,2] [,3]
[1,] FALSE FALSE TRUE
[2,] TRUE FALSE FALSE
[3,] FALSE FALSE FALSE
Example 3: Applying mathematical operation to NA values
If you add any numeric numbers to NA, then it will result in NA.
21 + NA
sqrt(NA)
NA + NA
Output
[1] NA
[1] NA
[1] NA
And we get the NA in all the outputs, but if we add NA + NaN, it will return NaN.
NaN + NA
Output
[1] NaN
Example 4: How to exclude NA values from the analysis
If you are calculating a mean of vector and that vector contains NA values, then you can exclude that NA value and calculate the mean of the remaining values. But if you don’t exclude the NA, it will return NA in the output.
rv <- c(1, 2, NA, 4, 5)
mean(rv)
Output
[1] NA
To exclude the NA value, pass the na.rm=TRUE as a second parameter in the mean() function.
rv <- c(1, 2, NA, 4, 5)
mean(rv, na.rm = TRUE)
Output
[1] 3
That means it has a calculated mean of 4 values(1, 2, 4, 5) whose sum is 12 and the mean is 3.
To remove the NA values from a vector, use the na.omit() function. The na.omit() method returns the object with listwise deletion of missing values.
rv <- c(1, 2, NA, 4, 5)
na.omit(rv)
Output
[1] 1 2 4 5
attr(,"na.action")
[1] 3
attr(,"class")
[1] "omit"
As you can see in the output that NA is omitted.

Krunal Lathiya is a Software Engineer with over eight years of experience. He has developed a strong foundation in computer science principles and a passion for problem-solving. In addition, Krunal has excellent knowledge of Data Science and Machine Learning, and he is an expert in R Language.