1. filter 함수 : 조건에 맞는 데이터만 추출하기

exam <- read.csv("csv_exam.csv")
dplyr::glimpse(exam)
## Observations: 20
## Variables: 5
## $ id      <int> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,...
## $ class   <int> 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, ...
## $ math    <int> 50, 60, 45, 30, 25, 50, 80, 90, 20, 50, 65, 45, 46, 48...
## $ english <int> 98, 97, 86, 98, 80, 89, 90, 78, 98, 98, 65, 85, 98, 87...
## $ science <int> 50, 60, 78, 58, 65, 98, 45, 25, 15, 45, 65, 32, 65, 12...

(1) 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

(2) class가 3반이 아닌 학생들의 데이터만 추출

exam %>% filter(class != 3)
##    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
## 7   7     2   80      90      45
## 8   8     2   90      78      25
## 9  13     4   46      98      65
## 10 14     4   48      87      12
## 11 15     4   75      56      78
## 12 16     4   58      98      65
## 13 17     5   65      68      98
## 14 18     5   80      78      90
## 15 19     5   89      68      87
## 16 20     5   78      83      58

(3) 수학점수가 50점을 초과한 경우

exam %>% filter(math > 50)
##    id class math english science
## 1   2     1   60      97      60
## 2   7     2   80      90      45
## 3   8     2   90      78      25
## 4  11     3   65      65      65
## 5  15     4   75      56      78
## 6  16     4   58      98      65
## 7  17     5   65      68      98
## 8  18     5   80      78      90
## 9  19     5   89      68      87
## 10 20     5   78      83      58

(4) 2반이면서 영어 점수가 80점 이상인 경우

exam %>% filter(class == 2 & english >= 80)
##   id class math english science
## 1  5     2   25      80      65
## 2  6     2   50      89      98
## 3  7     2   80      90      45

(5) 영어점수가 90점 이상이거나 과학점수가 90점 이상인 경우

exam %>% filter(english >= 90 | science >= 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   6     2   50      89      98
## 5   7     2   80      90      45
## 6   9     3   20      98      15
## 7  10     3   50      98      45
## 8  13     4   46      98      65
## 9  16     4   58      98      65
## 10 17     5   65      68      98
## 11 18     5   80      78      90

(6) 매치 연산자(matching operator, %in%) : 변수의 값이 지정한 조건 목록에 해당하는지 확인

exam %>% filter(class %in% c(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
## 7  11     3   65      65      65
## 8  12     3   45      85      32
## 9  17     5   65      68      98
## 10 18     5   80      78      90
## 11 19     5   89      68      87
## 12 20     5   78      83      58

(7) 추출한 행으로 데이터 만들기

  • 1반과 2반을 추출해 각 반의 수학평균점수 구하기
class1 <- exam %>% filter(class == 1)
class2 <- exam %>% filter(class == 2)
mean(class1$math)
## [1] 46.25
mean(class2$math)
## [1] 61.25

MPG 데이터를 이용한 분석

mpg <- as.data.frame(ggplot2::mpg) 
str(mpg)
## 'data.frame':    234 obs. of  11 variables:
##  $ manufacturer: chr  "audi" "audi" "audi" "audi" ...
##  $ model       : chr  "a4" "a4" "a4" "a4" ...
##  $ displ       : num  1.8 1.8 2 2 2.8 2.8 3.1 1.8 1.8 2 ...
##  $ year        : int  1999 1999 2008 2008 1999 1999 2008 1999 1999 2008 ...
##  $ cyl         : int  4 4 4 4 6 6 6 4 4 4 ...
##  $ trans       : chr  "auto(l5)" "manual(m5)" "manual(m6)" "auto(av)" ...
##  $ drv         : chr  "f" "f" "f" "f" ...
##  $ cty         : int  18 21 20 21 16 18 18 18 16 20 ...
##  $ hwy         : int  29 29 31 30 26 26 27 26 25 28 ...
##  $ fl          : chr  "p" "p" "p" "p" ...
##  $ class       : chr  "compact" "compact" "compact" "compact" ...

Q1. 자동차 배기량에 따라 고속도로 연비가 다른지 알아보려고 한다. displ(배기량)이 4 이하인 자동차와 5 이상인 자동차 중 어떤 자동차의 hwy(고속도로 연비)가 평균적으로 더 높은가?

displ.4 <- mpg %>%  filter(displ <= 4)
displ.5 <- mpg %>%  filter(displ >= 5)
mean(displ.4$hwy)
## [1] 25.96319
mean(displ.5$hwy)
## [1] 18.07895

Q2. 자동차 제조회사에 따라 도시 연비가 다른지 알아보려고 한다. “audi” 와 “toyota” 중 어느 manufacture(자동차 제조회사)의 cty(도시연비)가 평균적으로 높은가?

audi <- mpg %>% filter(manufacturer == "audi")
toyota <- mpg %>% filter(manufacturer == "toyota")
mean(audi$cyl)
## [1] 5.222222
mean(toyota$cyl)
## [1] 5.117647

Q3. “chevrolet”, “ford”, “honda” 자동차의 고속도로 연비 평균을 알아보려고 한다. 이 회사들의 데이터를 추출한 후 hwy 전체평균을 구하면?

manu.3 <- mpg %>% filter(manufacturer %in% c("chevrolet", "ford", "honda"))
mean(manu.3$hwy)
## [1] 22.50943