공간 객체에 데이터 추가

R로 지리공간 데이터 시각화하기

Charlotte Wickham

Assistant Professor at Oregon State University

ACS 소득 데이터

str(nyc_income)
'data.frame':    288 obs. of  6 variables:
 $ name    : chr  "Census Tract 1, New York County, New York" 
 "Census Tract 2.01, New York County, New York" 
 "Census Tract 2.02, New York County, New York" 
 "Census Tract 5, New York County, New York" ...
 $ state   : int  36 36 36 36 36 36 36 36 36 36 ...
 $ county  : int  61 61 61 61 61 61 61 61 61 61 ...
 $ tract   : chr  "000100" "000201" "000202" "000500" ...
 $ estimate: num  NA 23036 29418 NA 18944 ...
 $ se      : num  NA 3083 1877 NA 1442 ...
R로 지리공간 데이터 시각화하기

트랙트 폴리곤 데이터

str(nyc_tracts, max.level = 2)
Formal class 'SpatialPolygonsDataFrame' [package "sp"] with 5 slots
  ..@ data       :'data.frame':    288 obs. of  9 variables:
  ..@ polygons   :List of 288
  .. .. [list output truncated]
  ..@ plotOrder  : int [1:288] 175 225 97 209 249 232 208 247 244 207 ...
  ..@ bbox       : num [1:2, 1:2] -74 40.7 -73.9 40.9
  .. ..- attr(*, "dimnames")=List of 2
  ..@ proj4string:Formal class 'CRS' [package "sp"] with 1 slot
R로 지리공간 데이터 시각화하기

트랙트 폴리곤 데이터

str(nyc_tracts@data)
'data.frame':    288 obs. of  9 variables:
 $ STATEFP : chr  "36" "36" "36" "36" ...
 $ COUNTYFP: chr  "061" "061" "061" "061" ...
 $ TRACTCE : chr  "001401" "002201" "003200" "004000" ...
 $ AFFGEOID: chr  "1400000US36061001401" "1400000US36061002201" 
 "1400000US36061003200" "1400000US36061004000" ...
 $ GEOID   : chr  "36061001401" "36061002201" "36061003200" "36061004000" ...
 $ NAME    : chr  "14.01" "22.01" "32" "40" ...
 $ LSAD    : chr  "CT" "CT" "CT" "CT" ...
 $ ALAND   : num  93510 161667 217682 178340 124447 ...
 $ AWATER  : num  0 0 0 0 0 0 0 0 0 0 ...
  • 목표: 추정 중앙소득을 이 데이터 프레임에 추가합니다
R로 지리공간 데이터 시각화하기

폴리곤과 데이터의 대응

four_tracts
class       : SpatialPolygons 
features    : 4 
extent      : -73.99022, (xmin)
              -73.97875, (xmax)
              40.71413,  (ymin)
              40.73329   (ymax)
coord. ref. : +proj=longlat +datum=NAD83
              +no_defs +ellps=GRS80 
              +towgs84=0,0,0 
sapply(four_tracts@polygons,
       function(x) x@ID)
"156" "157" "158" "159"
four_data
    TRACTCE
159  004000
158  003200
157  002201
156  001401
R로 지리공간 데이터 시각화하기

폴리곤과 데이터의 대응

SpatialPolygonsDataFrame(Sr, data, match.ID = TRUE)

SpatialPolygonsDataFrame(four_tracts, four_data)
class       : SpatialPolygonsDataFrame 
features    : 4 
extent      : -73.99022, -73.97875, 40.71413, 40.73329  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=NAD83 +no_defs +ellps=GRS80 +towgs84=0,0,0 
variables   : 1
names       : TRACTCE 
min values  :  001401 
max values  :  004000
R로 지리공간 데이터 시각화하기

폴리곤과 데이터의 대응

SpatialPolygonsDataFrame(Sr, data, match.ID = TRUE)

SpatialPolygonsDataFrame(four_tracts, four_data)@data
    TRACTCE
156  001401
157  002201
158  003200
159  004000
R로 지리공간 데이터 시각화하기

폴리곤과 데이터의 대응

SpatialPolygonsDataFrame(four_tracts, four_data, match.ID = FALSE)@data
    TRACTCE
159  004000
158  003200
157  002201
156  001401

대응이 사라졌습니다!

R로 지리공간 데이터 시각화하기

새 데이터 추가

  • 생성 후에는 데이터와 폴리곤의 매칭이 유지되는지 검사하지 않습니다
  • 올바른 행에 맞춰 폴리곤을 주의 깊게 매칭해 객체를 다시 생성하십시오
  • sp 객체에는 sp::merge(), merge() 사용
R로 지리공간 데이터 시각화하기

연습해 봅시다!

R로 지리공간 데이터 시각화하기

Preparing Video For Download...