I would like to delete a few rows that have a specific character variable. I can do it but it is not efficient. The below code works but I would like a more efficient way.
Stuff2<-Stuff1[!Stuff1$State.Code=="PR",]
Stuff2<-Stuff2[!Stuff2$State.Code=="HI",]
Stuff2<-Stuff2[!Stuff2$State.Code=="AK",]
How do I create one line of code that removes all observations with PR, HI and AK? I see many examples of numeric values but none for character.
A solution with dplyr
:
library(dplyr)
Stuff2 %>%
filter(!State.Code %in% c("PR", "HI", "AK"))
# you remove if state.code is not in the character vector provided.
And with base R:
subset(Stuff2, !State.Code %in% c("PR", "HI", "AK"))