I am trying to allocate census data based on the block group level into school attendance zones. In using this code, the weights are based on the area of the block group covered by any attendance zone. However, at the edges of the map, there are places where block groups are partly in one school zone and partly not covered by any school zone. For example, a block group might be 20% in School Zone A, 30% in School Zone B, but 50% not covered by any school zone. The code above does not account for this missing 50% and therefore allocates all the residents of that block group into school zones A and B, rather than only 20% and 30% respectively.

Can anyone help me with adjusting this code such that the denominator for the block group weights reflects the total area of the block group?

Thanks for the prompt reply. I don't know how to read-in a reproducible example with GIS data, so I created a tiny version of the data and uploaded it to github: GitHub - ellyfield/Help

I assume the issue happens somewhere in the "aw_total" line, but I can't figure out how to get it to consider the entire block group as the total area, rather than just the segments of the block group covered by the zones.

The area weighting is not an overly difficult calculation; you could do it directly - meaning via actually calculating the areas using low level sf::st_area() and sf::st_intersection(), not relying on a package that introduces its own set of assumptions.

Thank you both! I think I've figured it out now, had some underlying data issues that were adding to the problem, but I've figured it out with these resources.