22

I have the following data frame lets call it df, with the following observations:

id   type   company
1    NA      NA
2    NA      ADM
3    North   Alex
4    South   NA
NA   North   BDA
6    NA      CA

I want to retain only the records which do not have NA in column "type" and "company".

id   type   company
3    North   Alex
NA   North   BDA

I tried:

 df_non_na <- df[!is.na(df$company) || !is.na(df$type), ]

But this did not work.

Thanks in advance

5
  • 2
    df [ complete.cases(df), ] ?
    – user5363218
    Commented Nov 4, 2015 at 11:37
  • 3
    Or the previous with a single | . ie: df[!is.na(df$company) | !is.na(df$type), ]
    – user5363218
    Commented Nov 4, 2015 at 11:38
  • I think this will remove the case where "id" is NA Commented Nov 4, 2015 at 11:38
  • 3
    Could also try library(data.table) ; na.omit(setDT(df), cols = c("type", "company")) Commented Nov 4, 2015 at 11:45
  • @ David, thanks for this Commented Nov 4, 2015 at 11:46

6 Answers 6

42

Using dplyr, you can also use the filter_at function

library(dplyr)
df_non_na <- df %>% filter_at(vars(type,company),all_vars(!is.na(.)))

all_vars(!is.na(.)) means that all the variables listed need to be not NA.

If you want to keep rows that have at least one value, you could do:

df_non_na <- df %>% filter_at(vars(type,company),any_vars(!is.na(.)))
1
  • 1
    This answer is good for cases where you want to apply this filter to all except certain columns, too. If you did (vars(-type,-company), for example, you'd be exempting the type and company variables from having to not be na.
    – Pake
    Commented Apr 11, 2021 at 16:37
22

We can get the logical index for both columns, use & and subset the rows.

df1[!is.na(df1$type) & !is.na(df1$company),]
# id  type company
#3  3 North    Alex
#5 NA North     BDA

Or use rowSums on the logical matrix (is.na(df1[-1])) to subset.

df1[!rowSums(is.na(df1[-1])),]
5
  • 1
    Dena answered this, all I had to do was use "|" instead of "||", Commented Nov 4, 2015 at 11:43
  • But 1 | doesn't give you your desired output. Commented Nov 4, 2015 at 11:45
  • @user3875610 I get 6 rows from df1[!is.na(df1$company) | !is.na(df1$type), ]
    – akrun
    Commented Nov 4, 2015 at 12:29
  • @akrun Why can't we use | here? and Why & ? I thought & specifies only if both columns have NA. Commented Jul 16, 2019 at 15:52
  • 1
    @MAPK It iss a bit of reverse logic. I guess it is the same logic from deMorgans law
    – akrun
    Commented Jul 16, 2019 at 16:00
17

You'll want to use drop_na()

library(dplyr)

new_df <- df %>% 
    drop_na(type, company)

10

The example with dplyr (version >= 1.0.4) and if_all(), since filter_at() is superseded

id <- c(1, 2, 3, 4, NA, 6)
type <- c(NA, NA, "North", "South", "North", NA)
company <- c(NA, "ADM", "Alex", NA, "BDA", "CA")

df <- tibble(id, type, company)

library(dplyr)

df_non_na <- df %>% filter(if_all(c(type,company), ~ !is.na(.)))
1
  • 1
    Using across()` in filter() is deprecated, use if_any() or if_all(). ` but it works with one of those depending on what you're after. Commented Jan 23, 2023 at 23:52
7

You need AND operator (&), not OR (|) I also strongly suggest the tidyverse approach by using the dplyr function filter() and the pipe operator %>%, from dplyr as well:

library(dplyr)
df_not_na <- df %>% filter(!is.na(company) & !is.na(type))
-5

you can use

na.omit(data_frame_name)
1
  • That will eliminate rows that have any NA values -- accepted answer already does the job, question has been resolved.
    – lefft
    Commented Feb 7, 2018 at 2:18

Your Answer

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

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