본문 바로가기
데이터분석/R

[R 데이터분석 with 샤이니] 지오 데이터프레임 만들기

by 버섯도리 2022. 7. 3.

> ## 06-2 주소와 좌표 결합하기

> # Step 1 : 데이터 불러오기

> setwd(dirname(rstudioapi::getSourceEditorContext()$path))
> load("./04_preprocess/04_preprocess.rdata")
> load("./05_geocoding/05_geocoding.rdata")

> # Step 2 : 주소와 좌표 결합하기

> library(dplyr)
> apt_price <- left_join(apt_price, juso_geocoding, by = c("juso_jibun" = "apt_juso"))
> apt_price <- na.omit(apt_price)


> ## 06-3 지오 데이터프레임 만들기

> # Step 1 : 지오 데이터프레임 생성하기

> install.packages("sp")

> library(sp)
coordinates(apt_price) <- ~coord_x + coord_y # 좌표값 할당
proj4string(apt_price) <- "+proj=longlat +datum=WGS84 +no_defs" # 좌표계(CRS) 정의
> install.packages('sf')
> library(sf)
> apt_price <- st_as_sf(apt_price)

> # Step 2 : 지오 데이터프레임 시각화

> install.packages("leaflet")

> library(leaflet)
> plot(apt_price$geometry, axes = T, pch = 1)
leaflet() %>%
+   addTiles() %>% # 오픈스트리트맵 불러오기
+   addCircleMarkers(data = apt_price[1:1000,], label = ~apt_nm) # 동그란 마커와 아파트 이름 표시

> # Step 3 : 지오 데이터프레임 저장하기

> dir.create("./06_geodataframe")
> save(apt_price, file = "./06_geodataframe/06_apt_price.rdata")
> write.csv(apt_price, "./06_geodataframe/06_apt_price.csv")

 

 

 

 

 

 

출처 : 김철민, ⌜공공데이터로 배우는 R 데이터분석 with 샤이니⌟, 이지스퍼블리싱, 2022