728x90
06 자유자재로 데이터 가공하기
06-1 데이터 전처리 - 원하는 형태로 데이터 가공하기
- 데이터 전처리(Data Preprocessing) : 분석에 적합하게 데이터를 가공하는 작업
- 일부를 추출하거나, 종류별로 나누거나, 여러 데이터를 합치는 등 데이터를 자유롭게 가공할 수 있어야 목적에 맞게 분석할 수 있음
- dplyr은 데이터 전처리 작업에 가장 많이 사용되는 패키지
- filter()
- 행 추출
- select()
- 열(변수) 추출
- arrange()
- 정렬
- mutate()
- 변수 추가
- summarise()
- 통계치 산출
- group_by()
- 집단별로 나누기
- left_join()
- 데이터 합치기(열)
- bind_rows()
- 데이터 합치기(행)
- filter()
06-2 조건에 맞는 데이터만 추출하기
library(dplyr)
exam <- read.csv("./data/csv_exam.csv")
exam
## id class math english science
## 1 1 1 50 98 50
## 2 2 1 60 97 60
## 3 3 1 45 86 78
## 4 4 1 30 98 58
## 5 5 2 25 80 65
## 6 6 2 50 89 98
## ...
# exam에서 class가 1인 경우만 추출해 출력
exam %>% filter(class == 1)
## id class math english science
## 1 1 1 50 98 50
## 2 2 1 60 97 60
## 3 3 1 45 86 78
## 4 4 1 30 98 58
# 1반이면서 수학 점수가 50점 이상인 경우
exam %>% filter(class == 1 & math >= 50)
## id class math english science
## 1 1 1 50 98 50
## 2 2 1 60 97 60
# 수학 점수가 90점 이상이거나 영어 점수가 90점 이상인 경우
exam %>% filter(math >= 90 | english >= 90)
## id class math english science
## 1 1 1 50 98 50
## 2 2 1 60 97 60
## 3 4 1 30 98 58
## 4 7 2 80 90 45
## 5 8 2 90 78 25
## 6 9 3 20 98 15
## ...
exam %>% filter(class %in% c(1, 3, 5)) # 1, 3, 5반에 해당하면 추출
## id class math english science
## 1 1 1 50 98 50
## 2 2 1 60 97 60
## 3 3 1 45 86 78
## 4 4 1 30 98 58
## 5 9 3 20 98 15
## 6 10 3 50 98 45
## ...
06-3 필요한 변수만 추출하기
exam %>% select(math) # math 추출
## math
## 1 50
## 2 60
## 3 45
## 4 30
## ...
exam %>% select(-math) # math 제외
## id class english science
## 1 1 1 98 50
## 2 2 1 97 60
## 3 3 1 86 78
## 4 4 1 98 58
## ...
exam %>%
filter(class == 1) %>% # class가 1인 행 추출
select(english) # english 추출
## english
## 1 98
## 2 97
## 3 86
## 4 98
exam %>%
select(id, math) %>% # id, math 추출
head(10) # 앞부분 10행까지 추출
## id math
## 1 1 50
## 2 2 60
## 3 3 45
## 4 4 30
## 5 5 25
## 6 6 50
## 7 7 80
## 8 8 90
## 9 9 20
## 10 10 50
06-4 순서대로 정렬하기
exam %>% arrange(math) # math 오름차순 정렬
## id class math english science
## 1 9 3 20 98 15
## 2 5 2 25 80 65
## 3 4 1 30 98 58
## 4 3 1 45 86 78
## 5 12 3 45 85 32
## 6 13 4 46 98 65
## ...
exam %>% arrange(desc(math)) # math 내림차순 정렬
## id class math english science
## 1 8 2 90 78 25
## 2 19 5 89 68 87
## 3 7 2 80 90 45
## 4 18 5 80 78 90
## 5 20 5 78 83 58
## 6 15 4 75 56 78
## ...
exam %>% arrange(class, math) # class 및 math 오름차순 정렬
## id class math english science
## 1 4 1 30 98 58
## 2 3 1 45 86 78
## 3 1 1 50 98 50
## 4 2 1 60 97 60
## 5 5 2 25 80 65
## 6 6 2 50 89 98
## 7 7 2 80 90 45
## 8 8 2 90 78 25
## ...
06-5 파생변수 추가하기
exam %>%
mutate(total = math + english + science) %>% # 총합 변수 추가
head # 일부 추출
## id class math english science total
## 1 1 1 50 98 50 198
## 2 2 1 60 97 60 217
## 3 3 1 45 86 78 209
## 4 4 1 30 98 58 186
## 5 5 2 25 80 65 170
## 6 6 2 50 89 98 237
exam %>%
mutate(test = ifelse(science >= 60, "pass", "fail")) %>%
head
## id class math english science test
## 1 1 1 50 98 50 fail
## 2 2 1 60 97 60 pass
## 3 3 1 45 86 78 pass
## 4 4 1 30 98 58 fail
## 5 5 2 25 80 65 pass
## 6 6 2 50 89 98 pass
exam %>%
mutate(total = math + english + science) %>% # 총합 변수 추가
arrange(total) %>% # 총합 변수 기준 정렬
head # 일부 추출
## id class math english science total
## 1 9 3 20 98 15 133
## 2 14 4 48 87 12 147
## 3 12 3 45 85 32 162
## 4 5 2 25 80 65 170
## 5 4 1 30 98 58 186
## 6 8 2 90 78 25 193
06-6 집단별로 요약하기
exam %>% summarise(mean_math = mean(math)) # math 평균 산출
## mean_math
## 1 57.45
exam %>%
group_by(class) %>% # class별로 분리
summarise(mean_math = mean(math)) # math 평균 산출
## # A tibble: 5 x 2
## class mean_math
## * <int> <dbl>
## 1 1 46.2
## 2 2 61.2
## 3 3 45
## 4 4 56.8
## 5 5 78
exam %>%
group_by(class) %>% # class별로 분리
summarise(mean_math = mean(math), # math 평균
sum_math = sum(math), # math 합계
median_math = median(math), # math 중앙값
n = n()) # 학생 수
## # A tibble: 5 x 5
## class mean_math sum_math median_math n
## * <int> <dbl> <int> <dbl> <int>
## 1 1 46.2 185 47.5 4
## 2 2 61.2 245 65 4
## 3 3 45 180 47.5 4
## 4 4 56.8 227 53 4
## 5 5 78 312 79 4
06-7 데이터 합치기
# 중간고사 데이터 생성
test1 <- data.frame(id = c(1, 2, 3, 4, 5),
midterm = c(60, 80, 70, 90, 85))
# 기말고사 데이터 생성
test2 <- data.frame(id = c(1, 2, 3, 4, 5),
final = c(70, 83, 65, 95, 80))
total <- left_join(test1, test2, by = "id") # id를 기준으로 합쳐 total에 할당
total # total 출력
## id midterm final
## 1 1 60 70
## 2 2 80 83
## 3 3 70 65
## 4 4 90 95
## 5 5 85 80
# 학생 1~5번 시험 데이터 생성
group_a <- data.frame(id = c(1, 2, 3, 4, 5),
test = c(60, 80, 70, 90, 85))
# 학생 6~10번 시험 데이터 생성
group_b <- data.frame(id = c(6, 7, 8, 9, 10),
test = c(70, 83, 65, 95, 80))
group_all <- bind_rows(group_a, group_b) # 데이터 합쳐서 group_all에 할당
group_all # group_all 출력
## id test
## 1 1 60
## 2 2 80
## 3 3 70
## 4 4 90
## 5 5 85
## 6 6 70
## 7 7 83
## ...
출처 : Do it! 쉽게 배우는 R 데이터 분석
728x90
'Do it! > R' 카테고리의 다른 글
Do it! 쉽게 배우는 R 데이터 분석 - 데이터 정제 (0) | 2021.02.25 |
---|---|
Do it! 쉽게 배우는 R 데이터 분석 - 데이터 프레임, 데이터 파악 (0) | 2021.02.23 |
Do it! 쉽게 배우는 R 데이터 분석 - R 기초 (0) | 2021.02.22 |