TeslaStat
TeslaStat

Reputation: 95

r, does not equal, nas are not included

I'm trying to filter my data to leave out certain values. The problem is that I want to include the NAs. When I use the does not equal, "!=", operator, the NAs are also removed.

    a= c("A","C","B",NA,"C","A",NA,"B","A")
    df = data.frame(a)

    df2 <- df %>%
       filter(a != "B")
    df2

For example, I would like df2 to include everything not equal to B (A and C) and NA instead of only A and C.

Upvotes: 6

Views: 24023

Answers (2)

moodymudskipper
moodymudskipper

Reputation: 47310

NA is never equal to anything.

NA == NA # NA, not TRUE

@bouncyball's would be the recommended solution, if you want to check if two values or variables are really the same you can use identical :

df %>% filter(!sapply(a, identical, "B"))

or using library purrr

df %>% filter(!map_lgl(a, identical, "B"))

Upvotes: 5

bouncyball
bouncyball

Reputation: 10761

We can include another condition in the filter function which will keep the NA values:

df %>%
   filter(a != "B" | is.na(a))

#      a
# 1    A
# 2    C
# 3 <NA>
# 4    C
# 5    A
# 6 <NA>
# 7    A

From ?NA

Logical computations treat NA as a missing `TRUE/FALSE value...

There's more to the explanation, but you can consult the help file.

Upvotes: 7

Related Questions