Home » How to Use na.omit in R (With Examples)

How to Use na.omit in R (With Examples)

by Tutor Aspire

You can use the na.omit() function in R to remove any incomplete cases in a vector, matrix, or data frame.

This function uses the following basic syntax:

#omit NA values from vector
x omit(x)

#omit rows with NA in any column of data frame
df omit(df)

#omit rows with NA in specific column of data frame
df !(is.na(df$column)), ]

The following examples show how to use this function in practice.

Example 1: Omit NA Values from Vector

The following code shows how to omit all NA values from a vector:

#define vector
x 

#omit NA values from vector
x omit(x)

x

[1]  1 24  6  9
attr(,"na.action")
[1] 3 5
attr(,"class")
[1] "omit"

The first line in the output shows the vector without NA values while the next two lines show additional information about the location of the NA values.

We can use the following code to just return the vector without the NA values:

#define vector
x 

#omit NA values from vector
x numeric(na.omit(x))

x

[1]  1 24  6  9

Example 2: Omit Rows with NA in Any Column of Data Frame

The following code shows how to omit all rows with NA values in any column of a data frame :

#define data frame
df frame(x=c(1, 24, NA, 6, NA, 9),
                 y=c(NA, 3, 4, 8, NA, 12),
                 z=c(NA, 7, 5, 15, 7, 14))

#view data frame
df

   x  y  z
1  1 NA NA
2 24  3  7
3 NA  4  5
4  6  8 15
5 NA NA  7
6  9 12 14

#omit rows with NA value in any column data frame
df omit(df)

#view data frame 
df

   x  y  z
2 24  3  7
4  6  8 15
6  9 12 14

Example 3: Omit Rows with NA in Specific Column of Data Frame

To omit rows with NA values in a specific column of a data frame, it’s actually easier to use the is.na() function as follows:

#define data frame
df frame(x=c(1, 24, NA, 6, NA, 9),
                 y=c(NA, 3, 4, 8, NA, 12),
                 z=c(NA, 7, 5, 15, 7, 14))

#view data frame
df

   x  y  z
1  1 NA NA
2 24  3  7
3 NA  4  5
4  6  8 15
5 NA NA  7
6  9 12 14

#remove rows with NA value in x column
df !(is.na(df$x)), ]

#view data frame 
df

   x  y  z
1  1 NA NA
2 24  3  7
4  6  8 15
6  9 12 14

Additional Resources

How to Use “Is Not NA” in R
How to Use is.null in R
How to Impute Missing Values in R
How to Replace NAs with Strings in R

You may also like