Scrabyard
Scrabyard

Reputation: 195

R Selection by value, avoid NA

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.

Upvotes: 2

Views: 315

Answers (4)

Ali
Ali

Reputation: 1080

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

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

Or with stringr:

str_subset(df$col1, "A")

Upvotes: 3

Waldi
Waldi

Reputation: 41220

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"

Upvotes: 1

akrun
akrun

Reputation: 887048

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"

Upvotes: 4

Ronak Shah
Ronak Shah

Reputation: 388907

Use %in% which returns FALSE for NA values.

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

Upvotes: 4

Related Questions