R Selection by value, avoid NA

Viewed 67

I try extract rows by values.

col1 <- c("A", "B", "C", NA)
df <- data.frame(col1)
df$col1[col1 == "A"]

The result in console is

> df$col1[col1 == "A"]
[1] "A" NA

But of course I only want "A". How to avoid R selecting NA values? By the way I think this behavior is pretty dangerous as many would run into this trap e.g. replacing values, subsetting, etc.

4 Answers

filter from dplyr automatically drops the NA elements even if the relational operator is ==

library(dplyr)
df %>%
   filter(col1 == 'A') %>% 
   pull(col1)
#[1] "A"

Or using the sbt (for subsetting) from collapse

library(collapse)
sbt(df, col1 == 'A')$col1
#[1] "A"

Use %in% which returns FALSE for NA values.

df$col1[col1 %in% "A"]
#[1] "A"

As Markus mentioned in the comments, a base solution is:

df$col1[which(df$col1 == "A")]

Or with stringr:

str_subset(df$col1, "A")

You could use is.element:

df[is.element(col1,"A"),]
#[1] "A"

Or simply filter out NA:

df[col1 == "A" & !is.na(col1),]
#[1] "A"
Related