r, does not equal, nas are not included

后端 未结 2 915
猫巷女王i
猫巷女王i 2021-01-22 04:19

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 rem

2条回答
  •  梦毁少年i
    2021-01-22 05:18

    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 
    # 4    C
    # 5    A
    # 6 
    # 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.

提交回复
热议问题