I want to filter out rows where a column contains a string. I am using a tidyverse
solution. The problem I'm having is str_detect
also seems to be finding NA
results and so these are also removed by my filter:
df1 = data.frame(x1 = c("PI", NA, "Yes", "text"),
x2 = as.character(c(NA, 1, NA,"text")),
x3 = c("foo", "bar","foo", "bar"))
> df1
x1 x2 x3
1 PI <NA> foo
2 <NA> 1 bar
3 Yes <NA> foo
4 text text bar
#remove rows which have "PI" in column `x1`:
df2 = df1%>%
filter(!str_detect(x1, "(?i)pi"))
> df2
x1 x2 x3
1 Yes <NA> foo
2 text text bar
How do I prevent str_detect
finding NA
?