Selecting list element based on function argument

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!

All the entries in the first data frame are either in counties that are wholly contained within the 17th district or in cities that are wholly contained in the district.

# data
cities <- 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"))
counties <- c("Carroll", "Rock Island", "Whiteside", "Knox")
dist <- data.frame(
  ID = c(1700007, 1700310, 1709400, 1715900, 1731020),
  District = c("Eastland CUSD 308", "West Carroll CUSD 314", "Chadwick-Milledgeville CUSD 399", "Freeport SD 145", "Pearl City CUSD 200"),
  County = c("Carroll", "Carroll", "Carroll", "Stephenson", "Stephenson"),
  City = c("Shannon", "Mount Carroll", "Milledgeville", "Freeport", "Pearl City")
)
partials <- vector()
for(i in seq_along(cities)) partials[i] = cities[i]
partials <- unlist(partials)
(hits <- dist[which(dist$County %in% counties | dist$City %in% partials),])
#>        ID                        District     County          City
#> 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
identical(dist,hits)
#> [1] TRUE

Created on 2023-01-20 with reprex v2.0.2

Thank you for the help! The partials vector worked great!

I should note, not all the school districts in my dataframe were in the 17th district. My bad, I should have made a better reprex.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.