20190718
2009년-2019년
1. 가구 수 불러오기
## Warning: package 'readxl' was built under R version 3.6.1
## Classes 'tbl_df', 'tbl' and 'data.frame': 187 obs. of 9 variables:
## $ 기간 : chr "2009" "2009" "2009" "2009" ...
## $ 가구주의 연령별: chr "합계" "19세이하" "20~24세" "25~29세" ...
## $ 계 : num 3521545 13221 95972 276594 350195 ...
## $ 1인 가구 : num 834678 9817 67297 160245 120648 ...
## $ 2인 가구 : num 761338 2515 22516 79881 95308 ...
## $ 3인 가구 : num 788211 571 4585 27266 80960 ...
## $ 4인 가구 : num 856685 175 1108 7128 42909 ...
## $ 5인 가구 : num 221487 133 415 1682 8342 ...
## $ 6인이상 가구 : num 59146 10 51 392 2028 ...
2. 합계만 가져오기
## [1] "기간" "가구주의 연령별" "계" "1인 가구"
## [5] "2인 가구" "3인 가구" "4인 가구" "5인 가구"
## [9] "6인이상 가구"
## Warning: package 'dplyr' was built under R version 3.6.1
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
3. long format -gather
library(tidyr)
house%>%
filter(`가구주의 연령별`=="합계")%>%
select(-c(`가구주의 연령별`,`계`))%>%
gather(menber,value,`1인 가구`:`6인이상 가구`)4. 비율- transmute
## Classes 'tbl_df', 'tbl' and 'data.frame': 11 obs. of 9 variables:
## $ 기간 : chr "2009" "2010" "2011" "2012" ...
## $ 가구주의 연령별: chr "합계" "합계" "합계" "합계" ...
## $ 계 : num 3521545 3573370 3633539 3656490 3691709 ...
## $ 1인 가구 : num 834678 881405 920414 951299 983558 ...
## $ 2인 가구 : num 761338 787589 813800 834146 857119 ...
## $ 3인 가구 : num 788211 801045 817509 824364 834664 ...
## $ 4인 가구 : num 856685 827032 812354 787815 766781 ...
## $ 5인 가구 : num 221487 212849 206358 197547 189705 ...
## $ 6인이상 가구 : num 59146 63450 63104 61319 59882 ...
h%>%
transmute( pro1=paste(round((`1인 가구`/`계`)*100,0),"%"),
pro2=paste(round((`2인 가구`/`계`)*100,0),"%"),
pro3=paste(round((`3인 가구`/`계`)*100,0),"%"),
pro4=paste(round((`4인 가구`/`계`)*100,0),"%"),
pro5=paste(round((`5인 가구`/`계`)*100,0),"%"),
pro6=paste(round((`6인이상 가구`/`계`)*100,0),"%"))5. plot
plot(h$`1인 가구`~ h$`기간`,type="b",col="red",ylim=c(min(h[-c(1,2,3)]),max(h[-c(1,2,3)])),xlab="연도",ylab="가구수")
lines(h$`2인 가구`~ h$`기간`, type="b",col="blue")
lines(h$`3인 가구`~ h$`기간`, type="b",col="forestgreen")
lines(h$`4인 가구`~ h$`기간`, type="b",col="purple")
lines(h$`5인 가구`~ h$`기간`, type="b",col="black")
lines(h$`6인이상 가구`~ h$`기간`, type="b",col="orange")
legend("topleft",legend=c("1인 가구","2인 가구","3인 가구","4인 가구","5인 가구","6인이상 가구"),col=c("red","blue","forestgreen","purple","black","orange"),lty=c(1,1,1,1,1,1),cex=0.5)
title("연도별 가구수")2000년-2019년
1. 가구 수 불러오기
## Classes 'tbl_df', 'tbl' and 'data.frame': 340 obs. of 9 variables:
## $ 기간 : chr "2000" "2000" "2000" "2000" ...
## $ 가구주의 연령별: chr "합계" "19세이하" "20~24세" "25~29세" ...
## $ 계 : num 3120733 11900 88458 275889 402084 ...
## $ 1인 가구 : num 508761 7258 51332 110246 77635 ...
## $ 2인 가구 : num 529620 3455 26850 88597 78940 ...
## $ 3인 가구 : num 678503 807 7546 53368 116913 ...
## $ 4인 가구 : num 1001594 232 1920 18396 103070 ...
## $ 5인 가구 : num 315871 138 733 4238 20296 ...
## $ 6인이상 가구 : num 86384 10 77 1044 5230 ...
2. 합계만 가져오기
## [1] "기간" "가구주의 연령별" "계" "1인 가구"
## [5] "2인 가구" "3인 가구" "4인 가구" "5인 가구"
## [9] "6인이상 가구"
3. long format -gather
library(tidyr)
house%>%
filter(`가구주의 연령별`=="합계")%>%
select(-c(`가구주의 연령별`,`계`))%>%
gather(menber,value,`1인 가구`:`6인이상 가구`)4. 비율- transmute
## Classes 'tbl_df', 'tbl' and 'data.frame': 20 obs. of 9 variables:
## $ 기간 : chr "2000" "2001" "2002" "2003" ...
## $ 가구주의 연령별: chr "합계" "합계" "합계" "합계" ...
## $ 계 : num 3120733 3159543 3211919 3262476 3307286 ...
## $ 1인 가구 : num 508761 552135 592715 633317 670410 ...
## $ 2인 가구 : num 529620 557128 583305 609880 634550 ...
## $ 3인 가구 : num 678503 686697 697714 708530 718536 ...
## $ 4인 가구 : num 1001594 983003 974370 963886 953426 ...
## $ 5인 가구 : num 315871 299687 287571 275333 263502 ...
## $ 6인이상 가구 : num 86384 80893 76244 71530 66862 ...
h%>%
transmute( pro1=paste(round((`1인 가구`/`계`)*100,0),"%"),
pro2=paste(round((`2인 가구`/`계`)*100,0),"%"),
pro3=paste(round((`3인 가구`/`계`)*100,0),"%"),
pro4=paste(round((`4인 가구`/`계`)*100,0),"%"),
pro5=paste(round((`5인 가구`/`계`)*100,0),"%"),
pro6=paste(round((`6인이상 가구`/`계`)*100,0),"%"))5. plot
plot(h$`1인 가구`~ h$`기간`,type="b",col="red",ylim=c(min(h[-c(1,2,3)]),max(h[-c(1,2,3)])),xlab="연도",ylab="가구수")
lines(h$`2인 가구`~ h$`기간`, type="b",col="blue")
lines(h$`3인 가구`~ h$`기간`, type="b",col="forestgreen")
lines(h$`4인 가구`~ h$`기간`, type="b",col="purple")
lines(h$`5인 가구`~ h$`기간`, type="b",col="black")
lines(h$`6인이상 가구`~ h$`기간`, type="b",col="orange")
legend("topleft",legend=c("1인 가구","2인 가구","3인 가구","4인 가구","5인 가구","6인이상 가구"),col=c("red","blue","forestgreen","purple","black","orange"),lty=c(1,1,1,1,1,1),cex=0.4)
title("연도별 가구수")