169

I want to know how to omit NA values in a data frame, but only in some columns I am interested in.

For example,

DF <- data.frame(x = c(1, 2, 3), y = c(0, 10, NA), z=c(NA, 33, 22))

but I only want to omit the data where y is NA, therefore the result should be

  x  y  z
1 1  0 NA
2 2 10 33

na.omit seems delete all rows contain any NA.

Can somebody help me out of this simple question?

But if now I change the question like:

DF <- data.frame(x = c(1, 2, 3,NA), y = c(1,0, 10, NA), z=c(43,NA, 33, NA))

If I want to omit only x=na or z=na, where can I put the | in function?

10 Answers 10

241

Use is.na

DF <- data.frame(x = c(1, 2, 3), y = c(0, 10, NA), z=c(NA, 33, 22))
DF[!is.na(DF$y),]
2
  • 3
    How do you apply this approach greedily on all columns in the data set? If any of the column value is NA skip. So your data set output is the second column only. Jul 18, 2017 at 15:35
  • 4
    Use na.omit to greedily remove all rows with NA in any column na.omit(DF)
    – M.Viking
    Aug 21, 2019 at 18:50
106

Hadley's tidyr just got this amazing function drop_na

library(tidyr)
DF %>% drop_na(y)
  x  y  z
1 1  0 NA
2 2 10 33
3
  • 8
    This method also allows you to specify more than one column (for dropping NA values). For instance, one could use DF %>% drop_na(y,z) to remove NA values in both columns, y, and z. Sep 23, 2020 at 9:59
  • @SolingerStuebchen can you pass a list for the columns to drop?
    – queste
    Mar 24 at 3:32
  • 1
    @queste yes, that is possible. You can do the following to drop NA values in multiple columns. First, define a list of column to be checked: drop_list <- c("y","z"). Second, you call DF%>% drop_na(drop_list). Apr 2 at 22:53
95

You could use the complete.cases function and put it into a function thusly:

DF <- data.frame(x = c(1, 2, 3), y = c(0, 10, NA), z=c(NA, 33, 22))

completeFun <- function(data, desiredCols) {
  completeVec <- complete.cases(data[, desiredCols])
  return(data[completeVec, ])
}

completeFun(DF, "y")
#   x  y  z
# 1 1  0 NA
# 2 2 10 33

completeFun(DF, c("y", "z"))
#   x  y  z
# 2 2 10 33

EDIT: Only return rows with no NAs

If you want to eliminate all rows with at least one NA in any column, just use the complete.cases function straight up:

DF[complete.cases(DF), ]
#   x  y  z
# 2 2 10 33

Or if completeFun is already ingrained in your workflow ;)

completeFun(DF, names(DF))
5
  • Can you make your approach greedy? Take all columns that do not have NAs at all. Jul 18, 2017 at 15:33
  • 1
    You mean just return rows with no NAs? Like completeFun(DF, names(DF))?
    – BenBarnes
    Jul 18, 2017 at 15:39
  • Correct! Please, consider adding it to your answer because it is a common need here. - - I think mnel's answer cannot be expanded as yours. Your function approach is great! Jul 18, 2017 at 15:43
  • 1
    Done! Thx for the tip @LéoLéopoldHertz준영
    – BenBarnes
    Jul 18, 2017 at 15:50
  • If you are viewing this past 2020 do yourself a favor and look at the more recent answers given below, for example the approach outlined by @amrrs below using drop_na() from tidyr does the same thing but is in my opinion a better solution today.
    – Ricky
    Oct 18, 2020 at 15:38
36

Use 'subset'

DF <- data.frame(x = c(1, 2, 3), y = c(0, 10, NA), z=c(NA, 33, 22))
subset(DF, !is.na(y))
17

It is possible to use na.omit for data.table:

na.omit(data, cols = c("x", "z"))
1
  • 7
    the cols= argument is available in the data.table::na.omit library. Not the base stats::na.omit.
    – M.Viking
    Aug 21, 2019 at 18:39
7

Omit row if either of two specific columns contain <NA>.

DF[!is.na(DF$x)&!is.na(DF$z),]
3

Try this:

cc=is.na(DF$y)
m=which(cc==c("TRUE"))
DF=DF[-m,]
2

Just try this:

DF %>% t %>% na.omit %>% t

It transposes the data frame and omits null rows which were 'columns' before transposition and then you transpose it back.

1
  • 12
    Please explain a bit what is going on.
    – vonbrand
    Aug 22, 2019 at 20:17
2

To update, a tidyverse approach with dplyr:

library(dplyr)

your_data_frame %>% 
  filter(!is.na(region_column))
0

You don't need to create a custom function with complete.cases to remove the rows with NA in a certain column. Here is a reproducible example:

DF <- data.frame(x = c(1, 2, 3), y = c(0, 10, NA), z=c(NA, 33, 22))
DF
#>   x  y  z
#> 1 1  0 NA
#> 2 2 10 33
#> 3 3 NA 22
DF[complete.cases(DF$y),]
#>   x  y  z
#> 1 1  0 NA
#> 2 2 10 33

Created on 2022-08-27 with reprex v2.0.2

As you can see, it removed the row with NA in certain column.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.