1

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.

Jessi
  • 48
  • 4

2 Answers2

0

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"))
RLave
  • 8,144
  • 3
  • 21
  • 37
0

Using regular expression and base R:

Stuff2[grepl("[^(PR|HI|AK)]",Stuff2$State.Code),]
emilliman5
  • 5,816
  • 3
  • 27
  • 37
A. Suliman
  • 12,923
  • 5
  • 24
  • 37