본문 바로가기

데이터 분석/R

Part Ⅸ: 한국 복지 패널 데이터 분석

[ 0. 데이터 읽어오기]

  1. Package 설치/로드

  2. 데이터 로드/검토

  3. 변수명 rename 작업

더보기
library(foreign)
library(dplyr)
library(readxl)
library(ggplot2)

raw_welfare <- read.spss("Data/Koweps_hpc10_2015_beta1.sav", to.data.frame = T)
welfare <- raw_welfare
head(welfare)
str(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)

- R 3.6 버전 대에서 구동되는 foreign 패키지가 CRAN에서 삭제되었다함 → R 4.xx 로 업데이트 하여 해결

 


[ ⅰ. 성별에 따른 월급 차이 ]

  1. 성별 변수 검토 및 전처리
  2. 월급변수 검토 및 전처리
성별 남자 여자 모름/무응답 월급 범위 모름/무응답
1 0 9 1 ~ 9998 9999
더보기
# Step 1: 성별 변수 검토 및 전처리
# 성별 변수 정보 확인
class(welfare$sex)
table(welfare$sex)

# 성별 변수 결측치 전처리
welfare$sex <- ifelse(welfare$sex==9, NA, welfare$sex)
table(is.na(welfare$sex))

# 성별 변수에 이름 부여
welfare$sex <- ifelse(welfare$sex==1, "Male", "Female")
table(welfare$sex)
qplot(welfare$sex)


# Step 2: 월급 변수 검토 및 전처리
# 월급 변수 정보 확인
class(welfare$income)
summary(welfare$income)
qplot(welfare$income) + xlim(0, 1000)

# 월급 변수 결측치 처리
welfare$income <- ifelse(welfare$income %in% c(0, 9999), NA, welfare%income)
table(is.na(welfare$income))

# 성별 별로 월급의 평균 산출
income_by_sex <- welfare %>%
  filter(!is.na(income)) %>%  
  group_by(sex) %>% 
  summarise(mean_income = mean(income))

#시각화
ggplot(data = income_by_sex, aes(x=sex, y=mean_income)) + geom_col()

 


[ ⅱ. 나이와 월급의 관계 ]

  1. 나이/월급 변수 검토 및 전처리
    • 파생변수 필요
  2. 나이에 따른 월급 평균표 → 시각화
더보기
# Step 1: 나이/월급 변수 검토 및 전처리
# 출생년도 정보 확인
class(welfare$birth)
summary(welfare$birth)

# 파생변수(나이) 추가
welfare$age <- 2015 - welfare$birth + 1
summary(welfare$age)
qplot(welfare$age)

# 데이터 정제
welfare$birth <- ifelse(welfare$birth == 9999, NA, welfare$birth)


# Step 2: 나이에 따른 월급 평균표
income_by_age <- welfare %>% 
  filter(!is.na(income)) %>% 
  group_by(age) %>% 
  summarise(mean_income = mean(income))

head(income_by_age)

# 시각화
ggplot(data = income_by_age, aes(x=age, y=mean_income)) + geom_line()

 


[ ⅲ. 연령대에 따른 월급 차이 ]

  1. 연령대 변수 검토 및 전처리

    • 파생변수 필요

  2. 연령대별 월급 차이 분석 → 시각화

    • x, y축 회전 기능 구현 필요

더보기
# Step 1: 연령대 변수 검토 및 전처리하기
# 파생변수(연령대) 추가
welfare <- welfare %>% 
  mutate(cohorts = ifelse(age < 30, "Z Gen", ifelse(age <= 59, "X/Y Gen", "BabyBoomer")))

table(welfare$cohorts)
qplot(welfare$cohorts)


# Step 2: 연령대에 따른 월급 차이 분석
cohorts_income <- welfare %>%
  filter(!is.na(income)) %>% 
  group_by(cohorts) %>% 
  summarise(mean_income=mean(income))

# 시각화
ggplot(data = cohorts_income, aes(x=cohorts, y=mean_income)) +
  geom_col() +
  scale_x_discrete(limits = c("Z Gen", "X/Y Gen", "BabyBoomer"))

 

 


[ ⅳ. 연령대 및 성별에 따른 월급 차이 ]

  1. 연령대 및 성별 월급 차이 분석하기

  2. 나이 및 성별 월급 차이 분석 → 시각화

    • geom_col 추가 기능 구현 필요

더보기
# Step 1: 연령대 및 성별 월급 차이 분석
income_by_cohorts_sex <- welfare %>% 
  filter(!is.na(income)) %>% 
  group_by(cohorts, sex) %>% 
  summarise(mean_income = mean(income))

income_by_cohorts_sex

# 한 컬럼 안에서 색 나누기
ggplot(data = income_by_cohorts_sex, aes(x=cohorts, y = mean_income, fill=sex)) +
  geom_col() +
  scale_x_discrete(limits = c("Z Gen", "X/Y Gen", "BabyBoomer"))

# 한 컬럼 두개로 쪼개기
ggplot(data = income_by_cohorts_sex, aes(x=cohorts, y = mean_income, fill=sex)) +
  geom_col(position="dodge") +
  scale_x_discrete(limits = c("Z Gen", "X/Y Gen", "BabyBoomer"))


# Step 2: 나이 및 성별 월급 차이 분석하기
income_by_sex_age <- welfare %>% 
  filter(!is.na(income)) %>% 
  group_by(age, sex) %>% 
  summarise(mean_income = mean(income))

income_by_sex_age

# 여러개의 그래프 시각화
ggplot(data = income_by_sex_age, aes(x=age, y=mean_income, col=sex)) +
  geom_line()

 


[ ⅴ. 직업별 월급 차이 ]

  1. 직업 변수 검토 및 전처리

    • left_join 활용

  2. 직업별 월급 차이 분석 → 시각화

더보기
# Step 1: 직업 변수 검토 및 전처리하기
class(welfare$code_job)
table(welfare$code_job)

# 직업명이 기재된 파일 로드
job_list <- read_excel("Data/Koweps_Codebook.xlsx", col_names = T, sheet = 2)
head(job_list)

# 기존 DF와 Join하기
welfare <- left_join(welfare, job_list, id="code_job")

# Join 결과 확인
welfare %>% 
  filter(!is.na(code_job)) %>% 
  select(code_job, job) %>% 
  head(10)


# Step 2: 직업별 월급 차이 분석하기
# 직업별 월급표 분석
income_by_job <- welfare %>% 
  filter(!is.na(job) & !is.na(income)) %>% 
  group_by(job) %>% 
  summarise(mean_income = mean(income))

# 월급 상위 10개 직업
top10 <- income_by_job %>% arrange(desc(mean_income)) %>% head(10)

ggplot(data = top10, aes(x=reorder(job, mean_income), y=mean_income)) +
  geom_col() +
  coord_flip()

# 월급 하위 10개 직업
bottom10 <- income_by_job %>% arrange(mean_income) %>% head(10)

ggplot(data = bottom10, aes(x=reorder(job, -mean_income), y=mean_income)) +
  geom_col() +
  coord_flip() +
  ylim(0, 850)

 


[ ⅵ. 성별 직업 빈도 ]

  1. 성별 직업 빈도 분석하기 → 시각화
더보기
# Step 1: 성별 직업빈도 분석
# 남성 직업 빈도표
job_male <- welfare %>%
  filter(!is.na(job) & sex=="Male") %>%
  group_by(job) %>%
  summarise(n = n()) %>% 
  arrange(desc(n)) %>% 
  head(10)

# 여성 직업 빈도표
job_female <- welfare %>%
  filter(!is.na(job) & sex=="Female") %>%
  group_by(job) %>%
  summarise(n = n()) %>% 
  arrange(desc(n)) %>% 
  head(10)

job_male
job_female

# 시각화
ggplot(data = job_male, aes(x=reorder(job, n), y=n)) +
  geom_col() +
  coord_flip()

ggplot(data = job_female, aes(x=reorder(job, n), y=n)) +
  geom_col() +
  coord_flip()

 


[ ⅶ. 종교 유무에 따른 이혼율 ]

[ ⅷ. 지역별 연령대 비율 ]

 

'데이터 분석 > R' 카테고리의 다른 글

Part ⅩⅠ: 지도 시각화  (0) 2021.01.11
Part Ⅹ: 텍스트 마이닝  (0) 2021.01.11
Part Ⅷ: 데이터 시각화  (0) 2021.01.07
Part Ⅶ: 데이터 정제(결측치 / 이상치)  (0) 2021.01.07
Part Ⅵ: 데이터 전처리  (0) 2021.01.06
Part Ⅴ: 데이터 분석 기초  (0) 2021.01.05