NA-based data frame filtering on multiple columns

I have the following data frame, let's call it dfwith 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 save only those records that do not have NA in the "type" and "company" columns.

id   type   company
3    North   Alex
NA   North   BDA

I did my best:

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

But that did not work.

thank you in advance

+6
source share
4 answers

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

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

Or use rowSumsin a logical matrix ( is.na(df1[-1])) for a subset.

df1[!rowSums(is.na(df1[-1])),]
+7
source

AND (&), OR (|). , dplyr() %>%, dplyr:

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

Using dplyr, you can also use the function filter_at

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

you can use

na.omit(data_frame_name)
-2
source

Source: https://habr.com/ru/post/1614439/


All Articles