Aggregating spatial feature objects

The following makes use of the sf package. You may need to install it from CRAN using the code install.packages("sf") if you want to run this on your computer. (The package is already installed on the notebook container, however.)

library(sf)
Linking to GEOS 3.9.0, GDAL 3.2.2, PROJ 7.2.1

The files “south-america-1990.RData”, “ged101.RData”, and “cshapes-1990.RData” where created by an earlier example.

load("south-america-1990.RData")
load("ged191.RData")
load("cshapes-1990.RData")
ged191_ellips <- st_transform(ged191,st_crs(cshapes.1990))
# Civilian deaths per country
aggregate(ged191_ellips["deaths_civilians"],by=SthAmCountries,sum)
although coordinates are longitude/latitude, st_intersects assumes that they are planar
Simple feature collection with 12 features and 1 field
geometry type:  MULTIPOLYGON
dimension:      XY
bbox:           xmin: -109.4461 ymin: -55.90223 xmax: -34.79292 ymax: 12.59027
CRS:            +proj=longlat +ellps=WGS84
First 10 features:
   deaths_civilians                       geometry
0                20 MULTIPOLYGON (((-58.17262 6...
1                NA MULTIPOLYGON (((-55.12796 5...
3               792 MULTIPOLYGON (((-66.11835 1...
6                 0 MULTIPOLYGON (((-71.8632 -4...
7                 0 MULTIPOLYGON (((-62.19884 -...
8               164 MULTIPOLYGON (((-52.16862 -...
9                NA MULTIPOLYGON (((-109.4092 -...
10               15 MULTIPOLYGON (((-91.65224 -...
11                0 MULTIPOLYGON (((-57.67267 -...
12             1021 MULTIPOLYGON (((-69.49973 -...
# Civilian deaths per country, with country names
within(
    aggregate(ged191_ellips["deaths_civilians"],by=SthAmCountries,sum),
    country <- SthAmCountries$CNTRY_NAME)
although coordinates are longitude/latitude, st_intersects assumes that they are planar
Simple feature collection with 12 features and 2 fields
geometry type:  MULTIPOLYGON
dimension:      XY
bbox:           xmin: -109.4461 ymin: -55.90223 xmax: -34.79292 ymax: 12.59027
CRS:            +proj=longlat +ellps=WGS84
First 10 features:
   deaths_civilians                       geometry   country
0                20 MULTIPOLYGON (((-58.17262 6...    Guyana
1                NA MULTIPOLYGON (((-55.12796 5...  Suriname
3               792 MULTIPOLYGON (((-66.11835 1... Venezuela
6                 0 MULTIPOLYGON (((-71.8632 -4... Argentina
7                 0 MULTIPOLYGON (((-62.19884 -...   Bolivia
8               164 MULTIPOLYGON (((-52.16862 -...    Brazil
9                NA MULTIPOLYGON (((-109.4092 -...     Chile
10               15 MULTIPOLYGON (((-91.65224 -...   Ecuador
11                0 MULTIPOLYGON (((-57.67267 -...  Paraguay
12             1021 MULTIPOLYGON (((-69.49973 -...      Peru
st_circ <- function(x,dist.km){
    dist.degr <- 360*dist.km/40007.863
    st_buffer(st_geometry(x),dist=dist.degr)
}
Bogota.region <- st_circ(Bogota,dist.km=200)
Colombia.rest <- st_difference(st_geometry(Colombia),Bogota.region)
Warning in st_buffer.sfc(st_geometry(x), dist = dist.degr):
st_buffer does not correctly buffer longitude/latitude data
dist is assumed to be in decimal degrees (arc_degrees).
although coordinates are longitude/latitude, st_difference assumes that they are planar
# Civilian deaths in the Bogota region and the rest of Colombia
as.data.frame(
aggregate(ged191_ellips["deaths_civilians"],
          by=c(Bogota.region,Colombia.rest),
          sum))
although coordinates are longitude/latitude, st_intersects assumes that they are planar
  deaths_civilians                       geometry
1             1021 POLYGON ((-72.30035 4.6, -7...
2             4994 MULTIPOLYGON (((-81.70473 1...