JamesW
JamesW

Reputation: 139

Filter on blanks or a certain value with R

I have a dataset I am taking from a public website using their R package and I have the full list. I am interested in a particular region of England, which is covered in the field ParentName. So, this below works:

mydata <- df[df$ParentName %in% c("South East region"),]

However, if I want to compare this to England overall, it does not have a value in ParentName, so I am looking to essentially filter on where ParentName is NULL/blank OR is the string value "South East region"

How might I go about that? I am extremely new to R and have had no training, so it is probably something very simple, but I can only seem to find guides on how to get rid of blanks! Thanks all.

I have tried using || but I don't think this works, or I am doing it wrong.

Upvotes: 0

Views: 710

Answers (1)

Vin&#237;cius F&#233;lix
Vin&#237;cius F&#233;lix

Reputation: 8811

If I understood correctly, this will work:

mydata <- 
 df[(df$ParentName == "South East region")|(df$ParentName == "")|(is.na(df$ParentName)),]

I recommend that you look at the dplyr package later, it will make data manipulation easier.

Upvotes: 1

Related Questions