How to select rows which has NA in any column in the dataframe in R?
3
0
Entering edit mode
8 weeks ago
Amr ▴ 140

How to select rows which has NA in any column in the dataframe in R?

for example:

enter image description here

dataframe NA R • 348 views
ADD COMMENT
3
Entering edit mode
8 weeks ago
LDT ▴ 210

you can use the

library(dplyr)

data %>%
 filter_all(any_vars(is.na(.)))
ADD COMMENT
1
Entering edit mode

filter_all and any_vars are deprecated. Instead use filter(df, if_any(everything(), is.na)).

ADD REPLY
0
Entering edit mode

Thanks genius

ADD REPLY
1
Entering edit mode
8 weeks ago
zx8754 11k

We could negate complete.cases:

data[ !complete.cases(data), ]
ADD COMMENT
1
Entering edit mode
8 weeks ago

No NAs:

data[rowSums(apply(data,1,is.na)) == 0,]

Any NAs:

data[rowSums(apply(data,1,is.na)) > 0,]
ADD COMMENT

Login before adding your answer.

Traffic: 1821 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6