簡體   English   中英

空間聚合與分組依據

[英]Spatial aggregation with a group by

我正在嘗試基於空間聚合計算按平均值分組。

我有兩個shapefile:人口普查區和病房。 這些病房的價值,我想根據每個人口普查區域的平均值進行平均。

這是shapfile:

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))

然后,我將普查區子集僅划分為病房中的那些:

census_tracts_in_wards <- census_tracts[wards, ]

我具有兩個級別的每個病房的數據:

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)

現在(最后問我一個問題),我想計算每個普查區的平均值,作為每個普查區中病房的匯總。 我認為這是我計算每個普查區均值的方式:

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

有沒有辦法通過做這個factor 我想要ag空間數據幀包含一個factor列和平均列value每個人口普查的。 本質上等同於:

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

但是,通過分組CTUIDcensus_tracts_in_wards代替AREA_IDwards

正如Pierre Lafortune所建議的那樣,公式語法在這里看起來很自然。 但是,這些都不起作用:

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)

也許該分組屬於FUN通話?

在Edzer Pebesma的提示下,仔細閱讀sp::aggregate文檔表明FUN應用於x的每個屬性。 因此,與其創建一個帶有因子列的長表,不如創建兩個單獨的列(每個因子一個)。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM