How to select rows which has NA in any column in the dataframe in R?
3
0
Entering edit mode
18 months ago
Amr ▴ 160

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

for example:

enter image description here

dataframe NA R • 1.6k views
ADD COMMENT
3
Entering edit mode
18 months ago
LDT ▴ 340

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
18 months ago
zx8754 11k

We could negate complete.cases:

data[ !complete.cases(data), ]
ADD COMMENT
1
Entering edit mode
18 months 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: 2929 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