Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

filtering data frame based on NA on multiple columns

Tags:

dataframe

r

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

like image 770
Anubhav Dikshit Avatar asked Nov 04 '15 11:11

Anubhav Dikshit


1 Answers

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(.)))
like image 59
Ricecakes Avatar answered Oct 18 '22 13:10

Ricecakes