I have a dataframe of Illinois school districts, and I'm trying to filter for districts in Illinois' 17th congressional district. Here is a sample of my dataframe, with the relevant columns shown:
`NCES ID` District County City
<chr> <chr> <chr> <chr>
1 1700007 Eastland CUSD 308 Carroll Shannon
2 1700310 West Carroll CUSD 314 Carroll Mount Carroll
3 1709400 Chadwick-Milledgeville CUSD 399 Carroll Milledgeville
4 1715900 Freeport SD 145 Stephenson Freeport
5 1731020 Pearl City CUSD 200 Stephenson Pearl City
The 17th district contains all of the following counties:
cd17_full_counties <- c("Carroll", "Rock Island", "Whiteside", "Knox")
and the listed cities in these counties:
# County # Cities
cd17_partial_counties <- list("Henry" = c("Colona", "Kewanee", "Galva"),
"Warren" = c("Alexis", "Monmouth"),
"McDonough" = c("Macomb", "Bardolph"),
"Mercer" = c("Burgess", "Matherville", "Viola"),
"Stephenson" = c("Freeport", "Pearl City", "Bolton"),
"Tazewell" = c("Delavan", "South Pekin"),
"McLean" = c("McLean", "Bloomington", "Normal"),
"Fulton" = c("Canton", "Cuba", "Farmington"),
"Peoria" = c("Hanna City", "Glasford", "Elmwood", "Peoria"))
I'm trying to write a function that will filter a row of my dataframe if the school district is in the 17th district; i.e., if its County
is in the cd17_full_counties
or its City
is in the list element for its County
in cd17_partial_counties
.
Here is my current attempt at that filter function:
in_cd17 <- function(county, city) {
return((county %in% cd17_full_counties) |
(city %in% cd17_partial_counties$county)) # not working
}
isbe_districts %>% filter(in_cd17(County, City))
When I filter my data using this function, the result only includes districts in the cd17_full_counties
, not those in the cd17_partial_counties
. It appears that cd17_partial_counties$county
is not using the county
argument in my function.
How do I select the list element based on the function argument county
?
Much thanks!