0

I have a df

ID <- c(101,102,103,104) 
Status <- c('P','F_Avg','F_Sig',NA) 
df <- data.frame(ID,Status)

I am trying to filter the failed ones and return both Pass and NA but I am not able to do so. I know it's a basic question but please bear with me and help me out.

I tried the following

df1 <-  sqldf("SELECT * FROM df
              WHERE Status NOT LIKE 'F%'")

and it returns only one observation and that is row1 which is 'P' but I also need row4 'NA'.

Sharath
  • 2,225
  • 3
  • 24
  • 37

2 Answers2

2
df1 <-  sqldf("SELECT * FROM df
              WHERE (Status NOT LIKE 'F%'
              OR Status IS null)")

Output:

   ID Status
1 101      P
2 104   <NA>

Using dplyr:

library(dplyr)
filter(df, !grepl("^F", Status))
mpalanco
  • 12,960
  • 2
  • 59
  • 67
1

Your sql must be this:

SELECT * FROM df
WHERE Status NOT LIKE 'F%'
or status = 'NA'

I'm not sure how you achieve that in r.

Dan Bracuk
  • 20,699
  • 4
  • 26
  • 43