> ## 04-1 불필요한 정보 지우기
>
> # Step 1 : 수집한 데이터 불러오기
>
> setwd(dirname(rstudioapi::getSourceEditorContext()$path))
> options(warn = -1)
>
> load("./03_integraed/03_apt_price.rdata")
> head(apt_price, 2)
X year month day price code dong_nm jibun con_year apt_nm area floor
1 1 2021 1 5 31,000 11680 역삼동 720-25 2002 대우디오빌 30.03 4
2 2 2021 1 6 61,000 11680 역삼동 766-8 2002 트레벨 33.48 3
>
> # Step 2 : 결측값과 공백 제거하기
>
> table(is.na(apt_price))
FALSE TRUE
520680 12
> apt_price <- na.omit(apt_price)
> table(is.na(apt_price))
FALSE
520548
>
> head(apt_price$price, 2)
[1] " 31,000" " 61,000"
>
> library(stringr)
> apt_price <- as.data.frame(apply(apt_price, 2, str_trim))
> head(apt_price$price, 2)
[1] "31,000" "61,000"
>
>
> ## 04-2 항목별 데이터 다듬기
>
> # Step 1 : 매매 연월일 만들기
>
> # install.packages("lubridate")
> library(lubridate)
> library(dplyr)
> apt_price <- apt_price %>% mutate(ymd=make_date(year, month, day))
> apt_price$ym <- floor_date(apt_price$ymd, "month")
> head(apt_price, 2)
X year month day price code dong_nm jibun con_year apt_nm area floor ymd ym
1 1 2021 1 5 31,000 11680 역삼동 720-25 2002 대우디오빌 30.0300 4 2021-01-05 2021-01-01
2 2 2021 1 6 61,000 11680 역삼동 766-8 2002 트레벨 33.4800 3 2021-01-06 2021-01-01
>
>
> # Step 2 : 매매가 변환하기
>
> head(apt_price$price, 3)
[1] "31,000" "61,000" "198,000"
> apt_price$price <- apt_price$price %>% sub(",","",.) %>% as.numeric()
> head(apt_price$price, 3)
[1] 31000 61000 198000
>
>
> # Step 3 : 주소 조합하기
>
> head(apt_price$apt_nm, 30)
...
[25] "역삼동하나빌" "역삼푸르지오" "래미안그레이튼(진달래2차)" "쌍용플레티넘밸류"
...
> apt_price$apt_nm <- gsub("\\(.*", "", apt_price$apt_nm) # 괄호 이후 삭제
> head(apt_price$apt_nm, 30)
...
[25] "역삼동하나빌" "역삼푸르지오" "래미안그레이튼" "쌍용플레티넘밸류" "이스턴오피스텔" "e-편한세상"
>
> loc <- read.csv("./01_code/sigun_code/sigun_code.csv", fileEncoding = "UTF-8")
> apt_price <- merge(apt_price, loc, by='code')
> apt_price$juso_jibun <- paste(apt_price$addr_2, apt_price$dong_nm, apt_price$jibun, apt_price$apt_nm)
> head(apt_price, 2)
code X year month day price dong_nm jibun con_year apt_nm area floor ymd ym sido
1 11110 1 2021 1 14 130000 청운동 56-45 2000 청운현대 129.7600 2 2021-01-14 2021-01-01 서울특별시
2 11110 2 2021 1 7 150000 사직동 9-1 2008 광화문스페이스본 144.5200 6 2021-01-07 2021-01-01 서울특별시
sigungu addr_1 addr_2 juso_jibun
1 종로구 서울_종로 서울특별시 종로구 서울특별시 종로구 청운동 56-45 청운현대
2 종로구 서울_종로 서울특별시 종로구 서울특별시 종로구 사직동 9-1 광화문스페이스본
>
> # Step 4 : 건축연도, 전용면적 변환하기
>
> head(apt_price$con_year, 3)
[1] "2000" "2008" "2004"
> apt_price$con_year <- apt_price$con_year %>% as.numeric()
> head(apt_price$con_year, 3)
[1] 2000 2008 2004
>
> head(apt_price$area, 3)
[1] "129.7600" "144.5200" "174.5500"
> apt_price$area <- apt_price$area %>% as.numeric() %>% round(0)
> head(apt_price$area, 3)
[1] 130 145 175
>
> # Step 5 : 평당 매매가 만들기
>
> apt_price$py <- round(((apt_price$price / apt_price$area) * 3.3), 0)
> head(apt_price[c("price","area","py")], 3)
price area py
1 130000 130 3300
2 150000 145 3414
3 175000 175 3300
>
> # Step 6 : 층수 변환하기
>
> summary(as.numeric(apt_price$floor))
Min. 1st Qu. Median Mean 3rd Qu. Max.
-3.000 4.000 8.000 9.146 13.000 66.000
> head(subset(apt_price, subset = as.numeric(floor) < 0))
code X year month day price dong_nm jibun con_year apt_nm area floor ymd ym sido
73 11110 18 2021 2 27 36000 숭인동 482 2002 동문 60 -1 2021-02-27 2021-02-01 서울특별시
653 11140 27 2021 3 11 9500 신당동 432-2006 2001 수산타운 33 -1 2021-03-11 2021-03-01 서울특별시
1240 11170 62 2021 2 3 76000 한남동 29-4 1974 한성1 65 -1 2021-02-03 2021-02-01 서울특별시
1337 11170 75 2021 3 15 37000 한남동 29-4 1974 한성1 25 -1 2021-03-15 2021-03-01 서울특별시
1395 11170 46 2021 4 10 89000 이촌동 193-3 1971 강변 66 -1 2021-04-10 2021-04-01 서울특별시
1867 11170 41 2021 9 15 103000 이촌동 193-3 1971 강변 66 -1 2021-09-15 2021-09-01 서울특별시
...
> head(subset(apt_price, subset = as.numeric(floor) < -1))
code X year month day price dong_nm jibun con_year apt_nm area floor ymd ym sido
32462 11590 166 2021 5 9 204000 흑석동 341 2019 아크로리버하임 85 -3 2021-05-09 2021-05-01 서울특별시
32463 11590 167 2021 5 9 204000 흑석동 341 2019 아크로리버하임 85 -3 2021-05-09 2021-05-01 서울특별시
32464 11590 168 2021 5 9 208000 흑석동 341 2019 아크로리버하임 85 -3 2021-05-09 2021-05-01 서울특별시
...
> apt_price$floor <- apt_price$floor %>% as.numeric() %>% abs() # 모두 양수로 바꾼다.
> summary(apt_price$floor)
Min. 1st Qu. Median Mean 3rd Qu. Max.
1.000 4.000 8.000 9.147 13.000 66.000
>
> apt_price$cnt <- 1 # 거래건수 변수 추가
>
>
> ## 04-3 전처리 데이터 저장하기
>
> # Step 1 : 필요한 컬럼만 추출하기
>
> apt_price <- apt_price %>% select(ymd, ym, year, code, addr_1, apt_nm,
+ juso_jibun, price, con_year, area, floor, py, cnt)
> head(apt_price, 2)
ymd ym year code addr_1 apt_nm juso_jibun price
1 2021-01-14 2021-01-01 2021 11110 서울_종로 청운현대 서울특별시 종로구 청운동 56-45 청운현대 130000
2 2021-01-07 2021-01-01 2021 11110 서울_종로 광화문스페이스본 서울특별시 종로구 사직동 9-1 광화문스페이스본 150000
con_year area floor py cnt
1 2000 130 2 3300 1
2 2008 145 6 3414 1
>
> # Step 2 : 전처리 데이터 저장하기
>
> setwd(dirname(rstudioapi::getSourceEditorContext()$path))
> dir.create("./04_preprocess")
> save(apt_price, file = "./04_preprocess/04_preprocess.rdata")
> write.csv(apt_price, "./04_preprocess/04_preprocess.csv")
출처 : 김철민, ⌜공공데이터로 배우는 R 데이터분석 with 샤이니⌟, 이지스퍼블리싱, 2022
'데이터분석 > R' 카테고리의 다른 글
[R 데이터분석 with 샤이니] 지오 데이터프레임 만들기 (0) | 2022.07.03 |
---|---|
[R 데이터분석 with 샤이니] 카카오맵 API로 지오 코딩하기 (0) | 2022.07.02 |
[R 데이터분석 with 샤이니] 자료 수집 : API 크롤러 만들기 (0) | 2022.06.29 |
[현장에서 바로 써먹는...] 텍스트마이닝 - 감성 분석 (0) | 2022.05.03 |
[현장에서 바로 써먹는...] 텍스트마이닝 - 워드클라우드 (0) | 2022.05.02 |