데이터 전처리

데이터 전처리는 데이터 분석 작업에 적합하게 데이터를 가공하는 일입니다

 

# dplyr 패키지의 주요함수

library(dplyr)

 

library(readxl)

excel_exam <- read_excel('excel_exam.xlsx', sheet= 'sheet2')

df_excel_exam <- as.data.frame(excel_exam)

df_excel_exam_copy <- df_excel_exam

 

# filter() : 행 단위 데이터를 추출합니다.

# %>%는 dplyr 패키지에서 사용하는 파이프연산자로 함수실행 결과를 다른 함수의 입력 데이터로 넘겨줄 수 있습니다.

df_excel_exam_copy %>% filter(df_excel_exam_copy$class2 == 1)

* 데이터 프레임이 %>% 연산자를 통해 함수로 넘어오기 때문에

특정 변수를 선택할 때 데이터 프레임 이름을 적지 않아도 됩니다.

df_excel_examcopy %>% filter(class2 == 1)

df_excel_examcopy %>% filter(class2 == 1 | class2 == 2 | class2 == 5)

매칭 연산자 in과 c() 함수를 이용하면 or 연산과 같은 효과를 낼 수 있습니다.

df_excel_examcopy %>% filter(class2 %in% c(1,2,5))

 

* mpg 데이터를 활용하기 위해 ggplot2 를 설치합니다.

install.packages('ggplot2')

library(ggplot2)

mpg

 

문제 1. 제조사(manufacturer)가 audi인 도시주행연비(cty)의 평균을 출력

mpg_audi <- mpg %>% filter(manufacturer == 'audi')

mean(mpg_audi$cty) # 17.61111

 

문제 2. 제조사(manufacturer)가 toyota인 도시주행연비(cty)의 형균을 출력

mpg_toyota <- mpg %>% filter(manufacturer == 'toyota')

mean(mpg_toyota$cty) # 18.52941

 

문제 3. 제조사가 chevrolet, ford, honda인 자동차의 고속도로 주행연비(hwy) 전체 평균을 출력

mpg_manufacturer <- mpg %>% filter(manufacturer %in% c('chevrolet', 'ford', 'honda'))

mean(mpg_manufacturer$hwy)

 

# table() : 데이터의 항목 (빈도수)

table(mpg_manufacturer$manufacturer)

 

# select() : 열 단위 데이터를 추출합니다.

df_excel_exam_copy %>% select(math2,english2,science2)

# select() 함수의 인수로 지정하는 변수 이름에 '-'를 붙여주면

'-'를 붙여준변수를 제외하고 나머지 데이터만 검색합니다.

 

df_excel_exam_copy %>% select(-math2,-science2)

# class2가 1인 데이터중 math2 과목만 출력합니다,

df_excel_exam_copy %>% filter(class2 == 1) %>% select(class2, math2)

df_excel_exam_copy %>% select(class2, math2) %>% filter(class2 == 1) * 순서는 상관 없다

 

# class2가 1, 3, 5인 데이터의 science2 열만 출력합니다.

df_excel_exam_copy %>% select(class2, science2) %>% filter(class2 %in% c(1,3,5))

 

# head() : 데이터 앞 부분의 데이터 개수를 정해서 볼 수 있습니다.

개수를 생략하면 기본값으로 6개의 데이터를 보여줍니다.

 

# tail() : 데이터 뒷 부분의 데이터 개수를 정해서 볼 수 있습니다

개수를 생략하면 기본값으로 6개의 데이터를 보여줍니다.

 

df_class135_science <- df_excel_exam_copy %>% select(class2, science2) %>% filter(class2 %in% c(1,3,5))

head(df_class135_science)

tail(df_class135_science)

head(df_class135_science, 3)

tail(df_class135_science, 3)

 

# arrange() : 데이터를 정렬합니다.

df_excel_exam_copy %>% arrange(math2) # 오름차순 정렬 

df_excel_exam_copy %>% arrange(desc(math2)) # 내림차순 정렬

 

# math2 점수로 내림차순 후 math2 점수가 같을 경우 science2 점수로 내림차순 정렬 후 5등까지 출력

df_excel_exam_copy %>% arrange(desc(math2), desc(science2)) %>% head(5)

 

# mpg 데이터에서 audi에서 생산한 자동차 중 hwy가 1~5위에 해당하는 차종을 출력

mpg %>% filter(manufacturer == 'audi') %>% arrange(desc(hwy)) %>% head(5)

 

# mutate() : 파생 변수를 여러개 추가할 수 있습니다.

df_excel_exam_copy %>% mutate(total = rowSums(subset(df_excel_exam_copy, select = math2 : science2)),

mean = rowmeans(subset(df_excel_exam_copy, select = math2 : science2)))

 

# 문제 mpg 데이터에서 cty와 hwy를 더한 합산 연비 변수를 만들고, 

합산 연비 변수의 평균 연비 변수를 만든뒤, 

평균 연비 변수의 값이 가장 큰 자동차 3개를 추출합니다. (제조사, 모델, 평균)

 

mpg %>% mutate( total = cty + hwy, mean = total / 2) %>% select(manufacturer, model, mean) %>% arrange(desc(mean)) %>% head(3)

 

# summarise() : 그룹별로 통계치를 산출할 수 있습니다.
# group_by() : 데이터의 그룹을 맺을 수 있습니다.

 

df_excel_exam_copy %>% mutate(mean_math = mean(math2))

df_excel_exam_copt %>% summarise(mean_math = mean(math2))

# 위 두식의 실행 결과를 보면 mutate() 함수는 mean_math라는 파생 변수를 만들고 모든 math2 데이터의 평균을 계산해 mean_math에 넣어주지만, summarise() 함수는 모든 math2 데이터의 평균만 계산해서 따로 출력합니다.

 

# group_by() 함수를 사용하여 먼저 그룹을 맺어주고 summarise()함수를 사용해 요약합니다.

df_excel_exam_copy %>% group_by(class2) %>% summarise(mean_math = mean(math))

 

df_excel_exam_copy %>% group_by(class2) %>% summarise(

 합계 = sum(math2),

 평균 = mean(math2),

 개수 = n(),

 최대값 = max(math2),

 최소값 = min(math2),

 표준편차 = sd(math2)

)

# 문제. 자동차회사별로 차종(class)이 suv인 자동차의 cty와 hwy의 평균을 계산해서 

내림차순으로 정렬하고 상위 5개를 출력합니다.

mpg %>% group_by(manufacturer) %>% filter(class == 'suv') %>% mutate( mean = (cty + hwy)/2)

%>% summarise(mean_avg = mean(mean)) %>% arrange(desc(mean_avg)) %>% head(5)

 

# 문제. 차종별(class)로 도시연비(cty)평균을 계산해서 평균이 높은 순서대로 출력합니다.

mpg %>% group_by(class) %>% summarise(cty_mean = mean(cty)) %>% arrange(desc(mean_cty))

 

# 문제. 고속도로 연비의 평균이 가장 높은 회사 3곳을 출력합니다.

mpg %>% group_by(manufacturer) %>% summarise(hwy_mean = mean(hwy)) %>% arrange(desc(hwy_mean)) %>% head(3)

 

# 문제. 각 회사별 경차(compact)의 차종 수를 내림차순으로 정렬해 출력합니다.

mpg %>% group_by(manufacturer) %>% filter(class == 'compact') %>% summarise(count = n()) %>% arrange( desc(count))

 

# left_join() : 가로로 데이터를 합칠 수 있습니다.

# left_join(데이터 프레임1, 데이터 프레임2, 데이터 프레임3 .. by ='기준변수명')

# by 옵션에는 합칠 때 기준이 되는 변수의 이름을 입력해야 하며 합쳐질 두 개의 데이터프레임에 반드시 같은 이름의 변수가 있어야 합니다

# by 옵셥을 지정하지 않으면 R이 알아서 같은 이름의 변수를 기준으로 합치기를 실행합니다.

test1 <- data.frame(id=c(1,2,3,4,5), middle = c(60,70,81,90,95))

test2 <- data.frame(id=c(1,2,3,4,5), midlle = c(70,83,63,95,80))

left_join(test1, test2, by = 'id')

 

df_excel_join <- df_excel_exam

df_teacher_name <- data.frame(class2=c(1,2,3,4,5), teacher = c('A','B','C','D','E'))

 

# left_join() 함수로 합치기를 실행할 때 두개의 데이터프레임에 행(데이터)의 개수가 반드시 같아야 할 필요는 없습니다.

left_join(df_excel_join, df_teacher_name, by='class2')

group1 <- data.frame(id=c(1,2,3,4,5),test = c(60,70,80,90,50))

group2 <- data.frame(id=c(6,7,8,9,10), test=c(70,90,30,50,55))

bind_rows(group1,group2)

 

# 데이터 정제

데이터 정제란 빠진 데이터 또는 잘못된 데이터를 제거하는 작업입니다

빠진 데이터, 측정이 안된 데이터, 없는 데이터 -> 결측치(NA)

# 문자열 결측치는 <NA>로 표시되고, 문자열을 제외한 나머지 자료형의 결측치는 NA로 표시됩니다.

df_na <- data.frame(gender=c('M','F',NA,'F','M'), score = c(5,4,3,2,NA))

 

# is.na() : 데이터에 결측치가 포함되어 있는지 여부를 확인할 수 있습니다.

# 결측치는 TRUE, 결측치가 아니며 FALSE로 표시됩니다.

is.na(df_na)

 

# is.na(), table() 이용해 결측치의 빈도수를 파악할 수 있습니다.

table(is.na(df_na))

table(is.na(df_na$gender))

table(is.na(df_na$score))

 

# 결측치가 포함된 데이터를 함수에 적용시키면 정상적으로 연산되지 않고 NA가 출력됩니다. 

따라서 정상적인 연산을 하려면 결측치를 제외해야 합니다.

sum(df_na$score)

mean(df_na$score)

# 결측치 처리방법
1. dplyr 패키지의 filter()를 사용해서 결측치를 제외한 데이터만 추출합니다.
gender에서 NA가 포함되고 score가 NA가 아닌 데이터만 추출합니다.

df_no_na <- df_na %>% filter(!is.na(score))

gender에서 NA가 아니고 score도 NA가 아닌 데이터만 추출합니다.

df_no_na <- df_na %>% filter(!is.na(gender) & !is.na(score))

 

sum(df_no_na$score)

mean(df_no_na$score)

 

2. na.omit() 함수를 사용해서 결측치가 있는 모든 행을 한꺼번에 제거할 수 있습니다.

df_no_na <- na.omit(df_na)

 

3. 함수를 실행할 때 na.rm=T 속성을 지정하면 결측치를 제외하고 함수를 실행합니다.

sum(df_na$score, na.rm = T)

mean(df_na$score, na.rm = T)

max(df_na$score, na.rm = T)

min(df_na$score, na.rm = T)

 

df_excel_exam_na <- df_excel_exam

df_excel_exam_na[c(3,8,15),'math2'] <- NA
df_excel_exam_na[20, 'science2'] <- NA

 

df_excel_exam_na %>% group_by(class2) %>% 
  summarise(
    math_sum = sum(math2, na.rm=T),

    math_n = n() * 개수에는 NA포함되어 출력 됩니다

)

 

# 4. 결측치의 개수도 세지 않으려면 filter() 함수를 사용하여 미리 결측치를 걸러내고 계산합니다.

df_excel_exam_na %>% group_by(class2) %>% filter(!is.na(math2)) %>%

 summarise(

  math_sum = sum(math2, na.rm = T),

  math_mean = mean(math2, na.rm = T),

  math_n = n() # 개수에 NA가 포함되지 않습니다.

+ Recent posts