KDBoom
KDBoom

Reputation: 37

Conditionally remove of rows in dataframe which includes NA

My example df:

  a1 a2 a3 a4
1  1  1  4  6
2  1  2  3  2
3  2 NA  5 NA
4  2  5  6  3
5  3  1  1  2
6  3  3  2  6

"If a4 == 6 then delete this row." So, I would like to delete (only!) row 1 and 6 in this example.

I know this works:

df_1 <- df[-c(1, 6), ]

But I'm looking for a more general solution.

I tried the most obvious way:

attach(df)
df_1 <- df[ which(a4 != 6),]
detach(df)

However, this deletes all NA as well and I would like to keep them.

  a1 a2 a3 a4
2  1  2  3  2
4  2  5  6  3
5  3  1  1  2

Then I tried:

df_1 <-df[!(df$a4 == 6),]

but then row 3 dances limbo and the whole row gets NA

   a1 a2 a3 a4
2   1  2  3  2
NA NA NA NA NA
4   2  5  6  3
5   3  1  1  2

Any ideas? Thank you in advance!

Upvotes: 1

Views: 1263

Answers (2)

Cath
Cath

Reputation: 24074

You can use %in% instead of == to properly handle NAs:

df[!(df$a4 %in% 6),]
#  a1 a2 a3 a4
#2  1  2  3  2
#3  2 NA  5 NA
#4  2  5  6  3
#5  3  1  1  2

Upvotes: 2

akrun
akrun

Reputation: 887048

We can use a logical index with is.na to remove

df[!(df$a4 == 6 & !is.na(df$a4)),]

as it will return the whole dataset when the element is not present

Or it can be written (as @thelatemail commented)

df[df$a4!=6 | (is.na(df$a4)),]

Upvotes: 2

Related Questions