options(repos = c(CRAN = "https://cloud.r-project.org"))
install.packages("foreign")
## 'C:/Users/ksu09/AppData/Local/R/win-library/4.3'의 위치에 패키지(들)을 설치합니다.
## (왜냐하면 'lib'가 지정되지 않았기 때문입니다)
## 패키지 'foreign'를 성공적으로 압축해제하였고 MD5 sums 이 확인되었습니다
## Warning: 패키지 'foreign'의 이전설치를 삭제할 수 없습니다
## Warning in file.copy(savedcopy, lib, recursive = TRUE):
## C:\Users\ksu09\AppData\Local\R\win-library\4.3\00LOCK\foreign\libs\x64\foreign.dll를
## C:\Users\ksu09\AppData\Local\R\win-library\4.3\foreign\libs\x64\foreign.dll로
## 복사하는데 문제가 발생했습니다: Permission denied
## Warning: 'foreign'를 복구하였습니다
##
## 다운로드된 바이너리 패키지들은 다음의 위치에 있습니다
## C:\Users\ksu09\AppData\Local\Temp\RtmpU7hIGy\downloaded_packages
library(foreign)
install.packages("dplyr")
## 'C:/Users/ksu09/AppData/Local/R/win-library/4.3'의 위치에 패키지(들)을 설치합니다.
## (왜냐하면 'lib'가 지정되지 않았기 때문입니다)
## 패키지 'dplyr'를 성공적으로 압축해제하였고 MD5 sums 이 확인되었습니다
## Warning: 패키지 'dplyr'의 이전설치를 삭제할 수 없습니다
## Warning in file.copy(savedcopy, lib, recursive = TRUE):
## C:\Users\ksu09\AppData\Local\R\win-library\4.3\00LOCK\dplyr\libs\x64\dplyr.dll를
## C:\Users\ksu09\AppData\Local\R\win-library\4.3\dplyr\libs\x64\dplyr.dll로
## 복사하는데 문제가 발생했습니다: Permission denied
## Warning: 'dplyr'를 복구하였습니다
##
## 다운로드된 바이너리 패키지들은 다음의 위치에 있습니다
## C:\Users\ksu09\AppData\Local\Temp\RtmpU7hIGy\downloaded_packages
library(dplyr)
## Warning: 패키지 'dplyr'는 R 버전 4.3.2에서 작성되었습니다
##
## 다음의 패키지를 부착합니다: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
install.packages("ggplot2")
## 'C:/Users/ksu09/AppData/Local/R/win-library/4.3'의 위치에 패키지(들)을 설치합니다.
## (왜냐하면 'lib'가 지정되지 않았기 때문입니다)
## 패키지 'ggplot2'를 성공적으로 압축해제하였고 MD5 sums 이 확인되었습니다
##
## 다운로드된 바이너리 패키지들은 다음의 위치에 있습니다
## C:\Users\ksu09\AppData\Local\Temp\RtmpU7hIGy\downloaded_packages
library(ggplot2)
## Warning: 패키지 'ggplot2'는 R 버전 4.3.2에서 작성되었습니다
library(readxl)
raw_welfare <- read.spss(file = "C:\\Users\\ksu09\\Downloads\\Koweps_hpc10_2015_beta1.sav", to.data.frame = T)
## Warning in read.spss(file =
## "C:\\Users\\ksu09\\Downloads\\Koweps_hpc10_2015_beta1.sav", :
## C:\Users\ksu09\Downloads\Koweps_hpc10_2015_beta1.sav: Compression bias (0) is
## not the usual value of 100
welfare <- raw_welfare
welfare <- rename(welfare, sex = h10_g3, birth = h10_g4, marriage = h10_g10, religion = h10_g11, income = p1002_8aq1, code_job = h10_eco9, code_region = h10_reg7)
welfare$age <- 2015 - welfare$birth + 1
summary(welfare$age)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 2.00 28.00 50.00 48.43 70.00 109.00
9-4 직접해보기
welfare <- welfare %>% mutate(ageg = ifelse(age < 30, "young", ifelse(age <= 59, "middle", "old")))
table(welfare$ageg)
##
## middle old young
## 6049 6281 4334
qplot(welfare$ageg)
## Warning: `qplot()` was deprecated in ggplot2 3.4.0.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.

ageg_income <- welfare %>% filter(!is.na(income)) %>% group_by(ageg) %>% summarise(mean_income = mean(income))
ageg_income
## # A tibble: 3 × 2
## ageg mean_income
## <chr> <dbl>
## 1 middle 281.
## 2 old 125.
## 3 young 164.
ggplot(data = ageg_income, aes(x = ageg, y = mean_income)) + geom_col()

ggplot(data = ageg_income, aes(x = ageg, y = mean_income)) + geom_col() + scale_x_discrete(limits = c("young", "middle", "old"))

9-5 직접해보기
sex_income <- welfare %>% filter(!is.na(income)) %>% group_by(ageg, sex) %>% summarise(mean_income = mean(income))
## `summarise()` has grouped output by 'ageg'. You can override using the
## `.groups` argument.
sex_income
## # A tibble: 6 × 3
## # Groups: ageg [3]
## ageg sex mean_income
## <chr> <dbl> <dbl>
## 1 middle 1 353.
## 2 middle 2 186.
## 3 old 1 174.
## 4 old 2 81.5
## 5 young 1 171.
## 6 young 2 160.
ggplot(data = sex_income, aes(x = ageg, y = mean_income, fill = sex)) +
geom_col() +
scale_x_discrete(limits = c("young", "middle", "old"))

ggplot(data = sex_income, aes(x = ageg, y = mean_income, fill = sex)) + geom_col(position = "dodge") + scale_x_discrete(limits = c("young", "middle", "old"))

sex_age <- welfare %>%
filter(!is.na(income)) %>%
group_by(age, sex) %>%
summarise(mean_income = mean(income))
## `summarise()` has grouped output by 'age'. You can override using the `.groups`
## argument.
head(sex_age)
## # A tibble: 6 × 3
## # Groups: age [3]
## age sex mean_income
## <dbl> <dbl> <dbl>
## 1 20 1 69
## 2 20 2 147.
## 3 21 1 102.
## 4 21 2 107.
## 5 22 1 118.
## 6 22 2 140.
ggplot(data = sex_age, aes(x = age, y = mean_income, col = sex)) + geom_line()

9-6 직접해보기
class(welfare$code_job)
## [1] "numeric"
table(welfare$code_job)
##
## 111 120 131 132 133 134 135 139 141 149 151 152 153 159 211 212
## 2 16 10 11 9 3 7 10 35 20 26 18 15 16 8 4
## 213 221 222 223 224 231 232 233 234 235 236 237 239 241 242 243
## 3 17 31 12 4 41 5 3 6 48 14 2 29 12 4 63
## 244 245 246 247 248 251 252 253 254 259 261 271 272 273 274 281
## 4 33 59 77 38 14 111 24 67 109 4 15 11 4 36 17
## 283 284 285 286 289 311 312 313 314 320 330 391 392 399 411 412
## 8 10 26 16 5 140 260 220 84 75 15 4 13 87 47 12
## 421 422 423 429 431 432 441 442 510 521 522 530 611 612 613 620
## 124 71 5 14 20 33 154 197 192 353 5 106 1320 11 40 2
## 630 710 721 722 730 741 742 743 751 752 753 761 762 771 772 773
## 20 29 30 22 16 27 3 34 34 5 49 69 27 11 61 86
## 774 780 791 792 799 811 812 819 821 822 823 831 832 841 842 843
## 7 17 5 21 45 16 1 6 9 9 23 5 17 32 10 4
## 851 852 853 854 855 861 862 863 864 871 873 874 875 876 881 882
## 19 13 7 33 9 3 14 17 31 2 257 34 37 2 2 3
## 891 892 899 910 921 922 930 941 942 951 952 953 991 992 999 1011
## 8 19 16 102 31 74 289 325 99 125 122 73 45 12 141 2
## 1012
## 17
library(readxl)
list_job <- read_excel("C:\\Users\\ksu09\\Downloads\\Koweps_Codebook.xlsx", col_names = T, sheet = 2)
head(list_job)
## # A tibble: 6 × 2
## code_job job
## <dbl> <chr>
## 1 111 의회의원 고위공무원 및 공공단체임원
## 2 112 기업고위임원
## 3 120 행정 및 경영지원 관리자
## 4 131 연구 교육 및 법률 관련 관리자
## 5 132 보험 및 금융 관리자
## 6 133 보건 및 사회복지 관련 관리자
dim(list_job)
## [1] 149 2