Spatial aggregation with a group by

南楼画角 提交于 2020-01-06 20:18:08

问题


I am trying to calculate grouped-by averages based on a spatial aggregation.

I have two shapefiles: census tracts and wards. The wards have a value that I would like to average by a factor for each census tract.

Here are the shapfiles:

library(dplyr)
library(rgeos)
library(rgdal)
# Census tracts
download.file("http://www12.statcan.gc.ca/census-recensement/2011/geo/bound-limit/files-fichiers/gct_000b11a_e.zip", 
    destfile = "gct_000a11a_e.zip")
unzip("gct_000a11a_e.zip", exdir="tracts") # corrected typo
census_tracts <- readOGR(dsn = "tracts", layer = "gct_000b11a_e") %>%
  spTransform(CRS('+init=epsg:4326'))

# Wards
download.file("http://opendata.toronto.ca/gcc/voting_subdivision_2010_wgs84.zip",
                destfile = "subdivisions_2010.zip")
unzip("subdivisions_2010.zip", exdir="wards")
wards <- readOGR(dsn = "wards", layer = "VOTING_SUBDIVISION_2010_WGS84") %>%
  spTransform(proj4string(census_tracts))

Then I subset the census tracts to just those in the wards:

census_tracts_in_wards <- census_tracts[wards, ]

I have data for each ward with a two-level factor:

df <- expand.grid(AREA_ID = wards$AREA_ID, factor = as.factor(letters[1:2]))
df$value <- rnorm(n = nrow(df))
wards@data <- left_join(wards@data, df)

Now (finally getting to my question) I would like to calculate the mean value in each census tract, as an aggregation of the wards within each census tract. I think this is how I calculate the mean for each census tract:

ag <- aggregate(x = wards["value"], by = census_tracts_in_wards, FUN = mean)

Is there a way to do this by factor? I'd like the ag spatial dataframe to include a factor column and a column for mean value of each census tract. Essentially the equivalent of:

result <- df %>% 
  group_by(AREA_ID, factor) %>% 
  summarize(value = mean(value))

But, grouped by CTUID from census_tracts_in_wards instead of AREA_ID in wards.

As suggested by Pierre Lafortune, the formula syntax seems natural here. But, none of these work:

ag2 <- aggregate(x = wards["value"] ~ wards["factor"], 
  by = census_tracts_in_wards, FUN = mean)
ag3 <- aggregate(x = wards["value" ~ "factor"], 
  by = census_tracts_in_wards, FUN = mean)
ag4 <- aggregate(x = wards["value ~ factor"], 
  by = census_tracts_in_wards, FUN = mean)

Perhaps the grouping belongs in the FUN call?


回答1:


Prompted by Edzer Pebesma, a closer read of the sp::aggregate documentation indicates that FUN is applied to each attribute of x. So, instead of creating a long table with a factor column, creating two separate columns (one for each factor) seems to work.

wards2 <- readOGR(dsn = "wards", layer = "VOTING_SUBDIVISION_2010_WGS84") %>%
  spTransform(proj4string(census_tracts))
wards2@data <- dplyr::select(wards2@data, AREA_ID) # Drop the other attributes
df2 <- tidyr::spread(df, factor, value)
wards2@data <- left_join(wards2@data, df2)
ag5 <- aggregate(x = wards2, by = census_tracts_in_wards, FUN = mean)
ag5@data <- dplyr::select(ag5@data, -(AREA_ID)) # The mean of AREA_ID is meaningless 
summary(ag5)
## Object of class SpatialPolygonsDataFrame
## Coordinates:
##         min       max
## x -79.73389 -79.08603
## y  43.56243  43.89091
## Is projected: FALSE 
## proj4string :
## [+init=epsg:4326 +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84
## +towgs84=0,0,0]
## Data attributes:
##        a                  b            
##  Min.   :-1.28815   Min.   :-1.835409  
##  1st Qu.:-0.24883   1st Qu.:-0.289510  
##  Median : 0.01048   Median : 0.008777  
##  Mean   : 0.02666   Mean   :-0.011018  
##  3rd Qu.: 0.25450   3rd Qu.: 0.265358  
##  Max.   : 1.92769   Max.   : 1.399876


来源:https://stackoverflow.com/questions/32618386/spatial-aggregation-with-a-group-by

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!