Selecting Data According To Patterns

Want to learn more? I recommend working through: R for Data Science, R Cookbook, and R Graphics Cookbook.

# create simulated district names
district <- factor(c("NORTH", "NORTHWEST", "CENTRAL", "SOUTH", "SOUTHWEST", "WESTERN"))
#Select all the cases where Baltimore's district name is "North"
district[grep("NORTH", as.character(district))]
[1] NORTH     NORTHWEST
Levels: CENTRAL NORTH NORTHWEST SOUTH SOUTHWEST WESTERN