dplyr Package

R Programming Basis

Description functions provided in dplyr Package

Yeongeun Jeon
06-25-2022

1. Package dplyr

Package dplyr (Ver. 1.0.9)는 Hadley Wichkham가 작성한 데이터 처리에 특화된 R 패키지이다. R에 입력된 데이터 세트는 대부분의 경우 바로 통계분석이 가능한 상태가 아니기 때문에 데이터를 분석하기 위해서는 원하는 형태로 가공하는 데이터 핸들링 기술이 필요하다. 통계 데이터 세트는 변수가 열, 관찰값이 행을 이루고 있는 2차원 구조를 가지고 있으며 데이터 프레임(Data Frame)으로 입력된다. Package dplyr는 이러한 데이터 프레임을 원하는 형태로 가공하고 처리하는 과정에 필요한 함수들을 포함한다.


1-1. Package dplyr의 장점

Package dplyr의 장점은 다음과 같다.


1-2. Package dplyr 내장 함수

Package dplyr의 대표적인 함수는 다음과 같다.


출처 : https://www.linkedin.com/pulse/cheat-sheet-list-functions-dplyr-package-r-janardan-chorat

그 외에도 Package dplyr에 포함된 함수는 dplyr 설명 pdf, dplyr 홈페이지, 그리고 Cheat Sheet를 참고한다.


2. Package dplyr 설치

# 1. 기본적인 패키지 설치와 로드 방법NA# install.packages("dplyr")
library(dplyr)

# 2. p_load 이용하여 설치와 로드를 한꺼번에 # install.packages("pacman")    # For p_load
pacman::p_load("dplyr")          # pacman::p_load : pacman 패키지의 p_load함수 사용NA

Result! 함수 p_load()는 작성된 패키지가 설치 되어있지 않으면 설치 후 함수 library()를 자동적으로 실행한다.


3. 파이프 연산자 %>%

# 데이터 불러오기
data(mtcars)

data <- mtcars
str(data)           # 데이터 구조
'data.frame':   32 obs. of  11 variables:
 $ mpg : num  21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...
 $ cyl : num  6 6 4 6 8 6 8 4 4 6 ...
 $ disp: num  160 160 108 258 360 ...
 $ hp  : num  110 110 93 110 175 105 245 62 95 123 ...
 $ drat: num  3.9 3.9 3.85 3.08 3.15 2.76 3.21 3.69 3.92 3.92 ...
 $ wt  : num  2.62 2.88 2.32 3.21 3.44 ...
 $ qsec: num  16.5 17 18.6 19.4 17 ...
 $ vs  : num  0 0 1 1 0 1 0 1 1 1 ...
 $ am  : num  1 1 1 0 0 0 0 0 0 0 ...
 $ gear: num  4 4 4 3 3 3 3 4 4 4 ...
 $ carb: num  4 4 1 1 2 1 4 2 2 4 ...

# 기본 함수 전처리
data1 <- select(mtcars, c(1, 3, 9))
data2 <- filter(data1, mpg >= 30)
data3 <- mutate(data2, new = mpg + disp)
data4 <- arrange(data3, new)
data4
                mpg disp am   new
Toyota Corolla 33.9 71.1  1 105.0
Honda Civic    30.4 75.7  1 106.1
Fiat 128       32.4 78.7  1 111.1
Lotus Europa   30.4 95.1  1 125.5
# 파이프 연산자 %>%를 활용한 전처리NAdata.pi <- data %>%
  select(c(1, 3, 9)) %>%
  filter(mpg >= 30) %>%
  mutate(new = mpg + disp) %>%
  arrange(new)

data.pi
                mpg disp am   new
Toyota Corolla 33.9 71.1  1 105.0
Honda Civic    30.4 75.7  1 106.1
Fiat 128       32.4 78.7  1 111.1
Lotus Europa   30.4 95.1  1 125.5

Result! “data4”와 “data.pi”는 동일한 결과를 출력하지만 파이프 연산자 %>%를 활용한 “data.pi”는 불필요한 객체를 생성하지 않았다.


4. 행을 대상으로 작업하는 함수

4-1. 함수 filter()

# 변수 mpg의 값이 30 이상인 행 선택NAdata %>%
  filter(mpg >= 30)
                mpg cyl disp  hp drat    wt  qsec vs am gear carb
Fiat 128       32.4   4 78.7  66 4.08 2.200 19.47  1  1    4    1
Honda Civic    30.4   4 75.7  52 4.93 1.615 18.52  1  1    4    2
Toyota Corolla 33.9   4 71.1  65 4.22 1.835 19.90  1  1    4    1
Lotus Europa   30.4   4 95.1 113 3.77 1.513 16.90  1  1    5    2

# 변수 mpg의 값이 30 이상이고 변수 wt의 값이 1.8 미만인 행 선택NAdata %>%
  filter(mpg >= 30, wt < 1.8)
              mpg cyl disp  hp drat    wt  qsec vs am gear carb
Honda Civic  30.4   4 75.7  52 4.93 1.615 18.52  1  1    4    2
Lotus Europa 30.4   4 95.1 113 3.77 1.513 16.90  1  1    5    2

Caution! 논리 연산자 & 대신에 콤마(,)를 사용할 수 있다.


# 변수 mpg의 값이 30 이하이고, 변수 cyl의 값이 6 또는 8이며, 변수 am의 값이 1인 행 선택NAdata %>%
  filter(mpg <= 30, cyl %in% c(6, 8), am ==1)
                mpg cyl disp  hp drat    wt  qsec vs am gear carb
Mazda RX4      21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
Mazda RX4 Wag  21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
Ford Pantera L 15.8   8  351 264 4.22 3.170 14.50  0  1    5    4
Ferrari Dino   19.7   6  145 175 3.62 2.770 15.50  0  1    5    6
Maserati Bora  15.0   8  301 335 3.54 3.570 14.60  0  1    5    8

# 변수 mpg의 값이 mpg의 중앙값과 Q3 사이에 있는 행 선택NAdata %>%
  filter(mpg >= median(mpg), mpg <= quantile(mpg, probs = 0.75))
                  mpg cyl  disp  hp drat    wt  qsec vs am gear carb
Mazda RX4        21.0   6 160.0 110 3.90 2.620 16.46  0  1    4    4
Mazda RX4 Wag    21.0   6 160.0 110 3.90 2.875 17.02  0  1    4    4
Datsun 710       22.8   4 108.0  93 3.85 2.320 18.61  1  1    4    1
Hornet 4 Drive   21.4   6 258.0 110 3.08 3.215 19.44  1  0    3    1
Merc 230         22.8   4 140.8  95 3.92 3.150 22.90  1  0    4    2
Merc 280         19.2   6 167.6 123 3.92 3.440 18.30  1  0    4    4
Toyota Corona    21.5   4 120.1  97 3.70 2.465 20.01  1  0    3    1
Pontiac Firebird 19.2   8 400.0 175 3.08 3.845 17.05  0  0    3    2
Ferrari Dino     19.7   6 145.0 175 3.62 2.770 15.50  0  1    5    6
Volvo 142E       21.4   4 121.0 109 4.11 2.780 18.60  1  1    4    2

Caution! 함수 median()은 중앙값을 계산하고, 함수 quantile(, prob)은 분위수를 계산하는 함수이다.


# 변수 mpg의 값이 30 이하이거나, 변수 wt의 값이 1.8 이하인 행 선택NAdata %>%
  filter(mpg <= 30 | wt <= 1.8)
                     mpg cyl  disp  hp drat    wt  qsec vs am gear
Mazda RX4           21.0   6 160.0 110 3.90 2.620 16.46  0  1    4
Mazda RX4 Wag       21.0   6 160.0 110 3.90 2.875 17.02  0  1    4
Datsun 710          22.8   4 108.0  93 3.85 2.320 18.61  1  1    4
Hornet 4 Drive      21.4   6 258.0 110 3.08 3.215 19.44  1  0    3
Hornet Sportabout   18.7   8 360.0 175 3.15 3.440 17.02  0  0    3
Valiant             18.1   6 225.0 105 2.76 3.460 20.22  1  0    3
Duster 360          14.3   8 360.0 245 3.21 3.570 15.84  0  0    3
Merc 240D           24.4   4 146.7  62 3.69 3.190 20.00  1  0    4
Merc 230            22.8   4 140.8  95 3.92 3.150 22.90  1  0    4
Merc 280            19.2   6 167.6 123 3.92 3.440 18.30  1  0    4
Merc 280C           17.8   6 167.6 123 3.92 3.440 18.90  1  0    4
Merc 450SE          16.4   8 275.8 180 3.07 4.070 17.40  0  0    3
Merc 450SL          17.3   8 275.8 180 3.07 3.730 17.60  0  0    3
Merc 450SLC         15.2   8 275.8 180 3.07 3.780 18.00  0  0    3
Cadillac Fleetwood  10.4   8 472.0 205 2.93 5.250 17.98  0  0    3
Lincoln Continental 10.4   8 460.0 215 3.00 5.424 17.82  0  0    3
Chrysler Imperial   14.7   8 440.0 230 3.23 5.345 17.42  0  0    3
Honda Civic         30.4   4  75.7  52 4.93 1.615 18.52  1  1    4
Toyota Corona       21.5   4 120.1  97 3.70 2.465 20.01  1  0    3
Dodge Challenger    15.5   8 318.0 150 2.76 3.520 16.87  0  0    3
AMC Javelin         15.2   8 304.0 150 3.15 3.435 17.30  0  0    3
Camaro Z28          13.3   8 350.0 245 3.73 3.840 15.41  0  0    3
Pontiac Firebird    19.2   8 400.0 175 3.08 3.845 17.05  0  0    3
Fiat X1-9           27.3   4  79.0  66 4.08 1.935 18.90  1  1    4
Porsche 914-2       26.0   4 120.3  91 4.43 2.140 16.70  0  1    5
Lotus Europa        30.4   4  95.1 113 3.77 1.513 16.90  1  1    5
Ford Pantera L      15.8   8 351.0 264 4.22 3.170 14.50  0  1    5
Ferrari Dino        19.7   6 145.0 175 3.62 2.770 15.50  0  1    5
Maserati Bora       15.0   8 301.0 335 3.54 3.570 14.60  0  1    5
Volvo 142E          21.4   4 121.0 109 4.11 2.780 18.60  1  1    4
                    carb
Mazda RX4              4
Mazda RX4 Wag          4
Datsun 710             1
Hornet 4 Drive         1
Hornet Sportabout      2
Valiant                1
Duster 360             4
Merc 240D              2
Merc 230               2
Merc 280               4
Merc 280C              4
Merc 450SE             3
Merc 450SL             3
Merc 450SLC            3
Cadillac Fleetwood     4
Lincoln Continental    4
Chrysler Imperial      4
Honda Civic            2
Toyota Corona          1
Dodge Challenger       2
AMC Javelin            2
Camaro Z28             4
Pontiac Firebird       2
Fiat X1-9              1
Porsche 914-2          2
Lotus Europa           2
Ford Pantera L         4
Ferrari Dino           6
Maserati Bora          8
Volvo 142E             2

4-2. 함수 slice()

# 5번째 행부터 10번째 행까지 선택NAdata %>%
  slice(5:10)
                   mpg cyl  disp  hp drat   wt  qsec vs am gear carb
Hornet Sportabout 18.7   8 360.0 175 3.15 3.44 17.02  0  0    3    2
Valiant           18.1   6 225.0 105 2.76 3.46 20.22  1  0    3    1
Duster 360        14.3   8 360.0 245 3.21 3.57 15.84  0  0    3    4
Merc 240D         24.4   4 146.7  62 3.69 3.19 20.00  1  0    4    2
Merc 230          22.8   4 140.8  95 3.92 3.15 22.90  1  0    4    2
Merc 280          19.2   6 167.6 123 3.92 3.44 18.30  1  0    4    4

# 5번째 행부터 10번째 행 제거NAdata%>%
  slice(-(5:10))
                     mpg cyl  disp  hp drat    wt  qsec vs am gear
Mazda RX4           21.0   6 160.0 110 3.90 2.620 16.46  0  1    4
Mazda RX4 Wag       21.0   6 160.0 110 3.90 2.875 17.02  0  1    4
Datsun 710          22.8   4 108.0  93 3.85 2.320 18.61  1  1    4
Hornet 4 Drive      21.4   6 258.0 110 3.08 3.215 19.44  1  0    3
Merc 280C           17.8   6 167.6 123 3.92 3.440 18.90  1  0    4
Merc 450SE          16.4   8 275.8 180 3.07 4.070 17.40  0  0    3
Merc 450SL          17.3   8 275.8 180 3.07 3.730 17.60  0  0    3
Merc 450SLC         15.2   8 275.8 180 3.07 3.780 18.00  0  0    3
Cadillac Fleetwood  10.4   8 472.0 205 2.93 5.250 17.98  0  0    3
Lincoln Continental 10.4   8 460.0 215 3.00 5.424 17.82  0  0    3
Chrysler Imperial   14.7   8 440.0 230 3.23 5.345 17.42  0  0    3
Fiat 128            32.4   4  78.7  66 4.08 2.200 19.47  1  1    4
Honda Civic         30.4   4  75.7  52 4.93 1.615 18.52  1  1    4
Toyota Corolla      33.9   4  71.1  65 4.22 1.835 19.90  1  1    4
Toyota Corona       21.5   4 120.1  97 3.70 2.465 20.01  1  0    3
Dodge Challenger    15.5   8 318.0 150 2.76 3.520 16.87  0  0    3
AMC Javelin         15.2   8 304.0 150 3.15 3.435 17.30  0  0    3
Camaro Z28          13.3   8 350.0 245 3.73 3.840 15.41  0  0    3
Pontiac Firebird    19.2   8 400.0 175 3.08 3.845 17.05  0  0    3
Fiat X1-9           27.3   4  79.0  66 4.08 1.935 18.90  1  1    4
Porsche 914-2       26.0   4 120.3  91 4.43 2.140 16.70  0  1    5
Lotus Europa        30.4   4  95.1 113 3.77 1.513 16.90  1  1    5
Ford Pantera L      15.8   8 351.0 264 4.22 3.170 14.50  0  1    5
Ferrari Dino        19.7   6 145.0 175 3.62 2.770 15.50  0  1    5
Maserati Bora       15.0   8 301.0 335 3.54 3.570 14.60  0  1    5
Volvo 142E          21.4   4 121.0 109 4.11 2.780 18.60  1  1    4
                    carb
Mazda RX4              4
Mazda RX4 Wag          4
Datsun 710             1
Hornet 4 Drive         1
Merc 280C              4
Merc 450SE             3
Merc 450SL             3
Merc 450SLC            3
Cadillac Fleetwood     4
Lincoln Continental    4
Chrysler Imperial      4
Fiat 128               1
Honda Civic            2
Toyota Corolla         1
Toyota Corona          1
Dodge Challenger       2
AMC Javelin            2
Camaro Z28             4
Pontiac Firebird       2
Fiat X1-9              1
Porsche 914-2          2
Lotus Europa           2
Ford Pantera L         4
Ferrari Dino           6
Maserati Bora          8
Volvo 142E             2

# 1번째, 4번째, 6번째 행 선택data %>%
  slice(c(1, 4, 6))
                mpg cyl disp  hp drat    wt  qsec vs am gear carb
Mazda RX4      21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
Hornet 4 Drive 21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
Valiant        18.1   6  225 105 2.76 3.460 20.22  1  0    3    1

Caution! 함수 slice() 뿐만 아니라 그와 관련된 다른 함수들도 행을 선택하는 데 유용하게 사용될 수 있다.

slice_head(data, n) 처음 n개 행을 선택
slice_tail(data, n) 마지막 n개 행을 선택
slice_sample(data, n, replace = FALSE) 비복원 추출을 이용하여 n개의 행을 선택
※ replace = TRUE : 복원 추출
slice_max(data, 변수, n) 특정 변수가 가장 큰 값을 갖는 n개의 행을 선택
slice_min(data, 변수, n) 특정 변수가 가장 작은 값을 갖는 n개의 행을 선택
# 함수 slice_head()data %>%
  slice_head(n = 6)
                   mpg cyl disp  hp drat    wt  qsec vs am gear carb
Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1

# 함수 slice_tail()data %>%
  slice_tail(n = 6)
                mpg cyl  disp  hp drat    wt qsec vs am gear carb
Porsche 914-2  26.0   4 120.3  91 4.43 2.140 16.7  0  1    5    2
Lotus Europa   30.4   4  95.1 113 3.77 1.513 16.9  1  1    5    2
Ford Pantera L 15.8   8 351.0 264 4.22 3.170 14.5  0  1    5    4
Ferrari Dino   19.7   6 145.0 175 3.62 2.770 15.5  0  1    5    6
Maserati Bora  15.0   8 301.0 335 3.54 3.570 14.6  0  1    5    8
Volvo 142E     21.4   4 121.0 109 4.11 2.780 18.6  1  1    4    2

# 함수 slice_sample() 비복원 추출NAdata %>%
  slice_sample(n = 10, replace = FALSE)
                     mpg cyl  disp  hp drat    wt  qsec vs am gear
Pontiac Firebird    19.2   8 400.0 175 3.08 3.845 17.05  0  0    3
Cadillac Fleetwood  10.4   8 472.0 205 2.93 5.250 17.98  0  0    3
Ferrari Dino        19.7   6 145.0 175 3.62 2.770 15.50  0  1    5
Hornet 4 Drive      21.4   6 258.0 110 3.08 3.215 19.44  1  0    3
Lincoln Continental 10.4   8 460.0 215 3.00 5.424 17.82  0  0    3
Merc 280C           17.8   6 167.6 123 3.92 3.440 18.90  1  0    4
Dodge Challenger    15.5   8 318.0 150 2.76 3.520 16.87  0  0    3
Toyota Corona       21.5   4 120.1  97 3.70 2.465 20.01  1  0    3
Mazda RX4 Wag       21.0   6 160.0 110 3.90 2.875 17.02  0  1    4
Mazda RX4           21.0   6 160.0 110 3.90 2.620 16.46  0  1    4
                    carb
Pontiac Firebird       2
Cadillac Fleetwood     4
Ferrari Dino           6
Hornet 4 Drive         1
Lincoln Continental    4
Merc 280C              4
Dodge Challenger       2
Toyota Corona          1
Mazda RX4 Wag          4
Mazda RX4              4

# 함수 slice_sample() 복원 추출NAdata %>%
  slice_sample(n = 10, replace = TRUE)
                     mpg cyl  disp  hp drat    wt  qsec vs am gear
Mazda RX4 Wag       21.0   6 160.0 110 3.90 2.875 17.02  0  1    4
Mazda RX4           21.0   6 160.0 110 3.90 2.620 16.46  0  1    4
Chrysler Imperial   14.7   8 440.0 230 3.23 5.345 17.42  0  0    3
Lincoln Continental 10.4   8 460.0 215 3.00 5.424 17.82  0  0    3
Duster 360          14.3   8 360.0 245 3.21 3.570 15.84  0  0    3
Toyota Corona       21.5   4 120.1  97 3.70 2.465 20.01  1  0    3
Merc 450SE...7      16.4   8 275.8 180 3.07 4.070 17.40  0  0    3
Merc 450SE...8      16.4   8 275.8 180 3.07 4.070 17.40  0  0    3
Fiat 128            32.4   4  78.7  66 4.08 2.200 19.47  1  1    4
Pontiac Firebird    19.2   8 400.0 175 3.08 3.845 17.05  0  0    3
                    carb
Mazda RX4 Wag          4
Mazda RX4              4
Chrysler Imperial      4
Lincoln Continental    4
Duster 360             4
Toyota Corona          1
Merc 450SE...7         3
Merc 450SE...8         3
Fiat 128               1
Pontiac Firebird       2

# 함수 slice_max()data %>%
  slice_max(mpg, n = 3)
                mpg cyl disp  hp drat    wt  qsec vs am gear carb
Toyota Corolla 33.9   4 71.1  65 4.22 1.835 19.90  1  1    4    1
Fiat 128       32.4   4 78.7  66 4.08 2.200 19.47  1  1    4    1
Honda Civic    30.4   4 75.7  52 4.93 1.615 18.52  1  1    4    2
Lotus Europa   30.4   4 95.1 113 3.77 1.513 16.90  1  1    5    2

# 함수 slice_min()data %>%
  slice_min(mpg, n = 3)
                     mpg cyl disp  hp drat    wt  qsec vs am gear
Cadillac Fleetwood  10.4   8  472 205 2.93 5.250 17.98  0  0    3
Lincoln Continental 10.4   8  460 215 3.00 5.424 17.82  0  0    3
Camaro Z28          13.3   8  350 245 3.73 3.840 15.41  0  0    3
                    carb
Cadillac Fleetwood     4
Lincoln Continental    4
Camaro Z28             4

4-3. 함수 arrange()

# 변수 mpg를 기준으로 오름차순 정렬data %>%
  arrange(mpg)
                     mpg cyl  disp  hp drat    wt  qsec vs am gear
Cadillac Fleetwood  10.4   8 472.0 205 2.93 5.250 17.98  0  0    3
Lincoln Continental 10.4   8 460.0 215 3.00 5.424 17.82  0  0    3
Camaro Z28          13.3   8 350.0 245 3.73 3.840 15.41  0  0    3
Duster 360          14.3   8 360.0 245 3.21 3.570 15.84  0  0    3
Chrysler Imperial   14.7   8 440.0 230 3.23 5.345 17.42  0  0    3
Maserati Bora       15.0   8 301.0 335 3.54 3.570 14.60  0  1    5
Merc 450SLC         15.2   8 275.8 180 3.07 3.780 18.00  0  0    3
AMC Javelin         15.2   8 304.0 150 3.15 3.435 17.30  0  0    3
Dodge Challenger    15.5   8 318.0 150 2.76 3.520 16.87  0  0    3
Ford Pantera L      15.8   8 351.0 264 4.22 3.170 14.50  0  1    5
Merc 450SE          16.4   8 275.8 180 3.07 4.070 17.40  0  0    3
Merc 450SL          17.3   8 275.8 180 3.07 3.730 17.60  0  0    3
Merc 280C           17.8   6 167.6 123 3.92 3.440 18.90  1  0    4
Valiant             18.1   6 225.0 105 2.76 3.460 20.22  1  0    3
Hornet Sportabout   18.7   8 360.0 175 3.15 3.440 17.02  0  0    3
Merc 280            19.2   6 167.6 123 3.92 3.440 18.30  1  0    4
Pontiac Firebird    19.2   8 400.0 175 3.08 3.845 17.05  0  0    3
Ferrari Dino        19.7   6 145.0 175 3.62 2.770 15.50  0  1    5
Mazda RX4           21.0   6 160.0 110 3.90 2.620 16.46  0  1    4
Mazda RX4 Wag       21.0   6 160.0 110 3.90 2.875 17.02  0  1    4
Hornet 4 Drive      21.4   6 258.0 110 3.08 3.215 19.44  1  0    3
Volvo 142E          21.4   4 121.0 109 4.11 2.780 18.60  1  1    4
Toyota Corona       21.5   4 120.1  97 3.70 2.465 20.01  1  0    3
Datsun 710          22.8   4 108.0  93 3.85 2.320 18.61  1  1    4
Merc 230            22.8   4 140.8  95 3.92 3.150 22.90  1  0    4
Merc 240D           24.4   4 146.7  62 3.69 3.190 20.00  1  0    4
Porsche 914-2       26.0   4 120.3  91 4.43 2.140 16.70  0  1    5
Fiat X1-9           27.3   4  79.0  66 4.08 1.935 18.90  1  1    4
Honda Civic         30.4   4  75.7  52 4.93 1.615 18.52  1  1    4
Lotus Europa        30.4   4  95.1 113 3.77 1.513 16.90  1  1    5
Fiat 128            32.4   4  78.7  66 4.08 2.200 19.47  1  1    4
Toyota Corolla      33.9   4  71.1  65 4.22 1.835 19.90  1  1    4
                    carb
Cadillac Fleetwood     4
Lincoln Continental    4
Camaro Z28             4
Duster 360             4
Chrysler Imperial      4
Maserati Bora          8
Merc 450SLC            3
AMC Javelin            2
Dodge Challenger       2
Ford Pantera L         4
Merc 450SE             3
Merc 450SL             3
Merc 280C              4
Valiant                1
Hornet Sportabout      2
Merc 280               4
Pontiac Firebird       2
Ferrari Dino           6
Mazda RX4              4
Mazda RX4 Wag          4
Hornet 4 Drive         1
Volvo 142E             2
Toyota Corona          1
Datsun 710             1
Merc 230               2
Merc 240D              2
Porsche 914-2          2
Fiat X1-9              1
Honda Civic            2
Lotus Europa           2
Fiat 128               1
Toyota Corolla         1

# 변수 mpg를 기준으로 오름차순 정렬하되 같은 값일 경우 변수 cyl을 기준으로 오름차순 정렬data %>%
  arrange(mpg, cyl)
                     mpg cyl  disp  hp drat    wt  qsec vs am gear
Cadillac Fleetwood  10.4   8 472.0 205 2.93 5.250 17.98  0  0    3
Lincoln Continental 10.4   8 460.0 215 3.00 5.424 17.82  0  0    3
Camaro Z28          13.3   8 350.0 245 3.73 3.840 15.41  0  0    3
Duster 360          14.3   8 360.0 245 3.21 3.570 15.84  0  0    3
Chrysler Imperial   14.7   8 440.0 230 3.23 5.345 17.42  0  0    3
Maserati Bora       15.0   8 301.0 335 3.54 3.570 14.60  0  1    5
Merc 450SLC         15.2   8 275.8 180 3.07 3.780 18.00  0  0    3
AMC Javelin         15.2   8 304.0 150 3.15 3.435 17.30  0  0    3
Dodge Challenger    15.5   8 318.0 150 2.76 3.520 16.87  0  0    3
Ford Pantera L      15.8   8 351.0 264 4.22 3.170 14.50  0  1    5
Merc 450SE          16.4   8 275.8 180 3.07 4.070 17.40  0  0    3
Merc 450SL          17.3   8 275.8 180 3.07 3.730 17.60  0  0    3
Merc 280C           17.8   6 167.6 123 3.92 3.440 18.90  1  0    4
Valiant             18.1   6 225.0 105 2.76 3.460 20.22  1  0    3
Hornet Sportabout   18.7   8 360.0 175 3.15 3.440 17.02  0  0    3
Merc 280            19.2   6 167.6 123 3.92 3.440 18.30  1  0    4
Pontiac Firebird    19.2   8 400.0 175 3.08 3.845 17.05  0  0    3
Ferrari Dino        19.7   6 145.0 175 3.62 2.770 15.50  0  1    5
Mazda RX4           21.0   6 160.0 110 3.90 2.620 16.46  0  1    4
Mazda RX4 Wag       21.0   6 160.0 110 3.90 2.875 17.02  0  1    4
Volvo 142E          21.4   4 121.0 109 4.11 2.780 18.60  1  1    4
Hornet 4 Drive      21.4   6 258.0 110 3.08 3.215 19.44  1  0    3
Toyota Corona       21.5   4 120.1  97 3.70 2.465 20.01  1  0    3
Datsun 710          22.8   4 108.0  93 3.85 2.320 18.61  1  1    4
Merc 230            22.8   4 140.8  95 3.92 3.150 22.90  1  0    4
Merc 240D           24.4   4 146.7  62 3.69 3.190 20.00  1  0    4
Porsche 914-2       26.0   4 120.3  91 4.43 2.140 16.70  0  1    5
Fiat X1-9           27.3   4  79.0  66 4.08 1.935 18.90  1  1    4
Honda Civic         30.4   4  75.7  52 4.93 1.615 18.52  1  1    4
Lotus Europa        30.4   4  95.1 113 3.77 1.513 16.90  1  1    5
Fiat 128            32.4   4  78.7  66 4.08 2.200 19.47  1  1    4
Toyota Corolla      33.9   4  71.1  65 4.22 1.835 19.90  1  1    4
                    carb
Cadillac Fleetwood     4
Lincoln Continental    4
Camaro Z28             4
Duster 360             4
Chrysler Imperial      4
Maserati Bora          8
Merc 450SLC            3
AMC Javelin            2
Dodge Challenger       2
Ford Pantera L         4
Merc 450SE             3
Merc 450SL             3
Merc 280C              4
Valiant                1
Hornet Sportabout      2
Merc 280               4
Pontiac Firebird       2
Ferrari Dino           6
Mazda RX4              4
Mazda RX4 Wag          4
Volvo 142E             2
Hornet 4 Drive         1
Toyota Corona          1
Datsun 710             1
Merc 230               2
Merc 240D              2
Porsche 914-2          2
Fiat X1-9              1
Honda Civic            2
Lotus Europa           2
Fiat 128               1
Toyota Corolla         1

# 변수 mpg를 기준으로 내림차순 정렬정렬
data %>%
  arrange(desc(mpg))
                     mpg cyl  disp  hp drat    wt  qsec vs am gear
Toyota Corolla      33.9   4  71.1  65 4.22 1.835 19.90  1  1    4
Fiat 128            32.4   4  78.7  66 4.08 2.200 19.47  1  1    4
Honda Civic         30.4   4  75.7  52 4.93 1.615 18.52  1  1    4
Lotus Europa        30.4   4  95.1 113 3.77 1.513 16.90  1  1    5
Fiat X1-9           27.3   4  79.0  66 4.08 1.935 18.90  1  1    4
Porsche 914-2       26.0   4 120.3  91 4.43 2.140 16.70  0  1    5
Merc 240D           24.4   4 146.7  62 3.69 3.190 20.00  1  0    4
Datsun 710          22.8   4 108.0  93 3.85 2.320 18.61  1  1    4
Merc 230            22.8   4 140.8  95 3.92 3.150 22.90  1  0    4
Toyota Corona       21.5   4 120.1  97 3.70 2.465 20.01  1  0    3
Hornet 4 Drive      21.4   6 258.0 110 3.08 3.215 19.44  1  0    3
Volvo 142E          21.4   4 121.0 109 4.11 2.780 18.60  1  1    4
Mazda RX4           21.0   6 160.0 110 3.90 2.620 16.46  0  1    4
Mazda RX4 Wag       21.0   6 160.0 110 3.90 2.875 17.02  0  1    4
Ferrari Dino        19.7   6 145.0 175 3.62 2.770 15.50  0  1    5
Merc 280            19.2   6 167.6 123 3.92 3.440 18.30  1  0    4
Pontiac Firebird    19.2   8 400.0 175 3.08 3.845 17.05  0  0    3
Hornet Sportabout   18.7   8 360.0 175 3.15 3.440 17.02  0  0    3
Valiant             18.1   6 225.0 105 2.76 3.460 20.22  1  0    3
Merc 280C           17.8   6 167.6 123 3.92 3.440 18.90  1  0    4
Merc 450SL          17.3   8 275.8 180 3.07 3.730 17.60  0  0    3
Merc 450SE          16.4   8 275.8 180 3.07 4.070 17.40  0  0    3
Ford Pantera L      15.8   8 351.0 264 4.22 3.170 14.50  0  1    5
Dodge Challenger    15.5   8 318.0 150 2.76 3.520 16.87  0  0    3
Merc 450SLC         15.2   8 275.8 180 3.07 3.780 18.00  0  0    3
AMC Javelin         15.2   8 304.0 150 3.15 3.435 17.30  0  0    3
Maserati Bora       15.0   8 301.0 335 3.54 3.570 14.60  0  1    5
Chrysler Imperial   14.7   8 440.0 230 3.23 5.345 17.42  0  0    3
Duster 360          14.3   8 360.0 245 3.21 3.570 15.84  0  0    3
Camaro Z28          13.3   8 350.0 245 3.73 3.840 15.41  0  0    3
Cadillac Fleetwood  10.4   8 472.0 205 2.93 5.250 17.98  0  0    3
Lincoln Continental 10.4   8 460.0 215 3.00 5.424 17.82  0  0    3
                    carb
Toyota Corolla         1
Fiat 128               1
Honda Civic            2
Lotus Europa           2
Fiat X1-9              1
Porsche 914-2          2
Merc 240D              2
Datsun 710             1
Merc 230               2
Toyota Corona          1
Hornet 4 Drive         1
Volvo 142E             2
Mazda RX4              4
Mazda RX4 Wag          4
Ferrari Dino           6
Merc 280               4
Pontiac Firebird       2
Hornet Sportabout      2
Valiant                1
Merc 280C              4
Merc 450SL             3
Merc 450SE             3
Ford Pantera L         4
Dodge Challenger       2
Merc 450SLC            3
AMC Javelin            2
Maserati Bora          8
Chrysler Imperial      4
Duster 360             4
Camaro Z28             4
Cadillac Fleetwood     4
Lincoln Continental    4

4-4. 함수 distinct()

df1 <- data.frame(id = rep(1:3, times = 2:4), x1 = c(1:2, 1:3, 1:4))
df1
  id x1
1  1  1
2  1  2
3  2  1
4  2  2
5  2  3
6  3  1
7  3  2
8  3  3
9  3  4

# 변수 id의 값 중 중복된 값 제거df1 %>%
  distinct(id)
  id
1  1
2  2
3  3

Result! 변수 “id”의 값 중 중복된 값이 제거되어 1, 2, 3이 출력된다.


# 변수 id의 값 중 중복된 값 제거하되, 모든 변수 유지
df1 %>%
  distinct(id, .keep_all = TRUE)
  id x1
1  1  1
2  2  1
3  3  1

Result! 변수 “id”의 값 중 중복된 값이 제거되고 중복값이 처음 나타난 행이 유지되었다.


4-5. 함수 bind_rows()

# 열의 길이가 다른 두 데이터 프레임 결합NAx <- data.frame(x1 = 1:4, x2 = c("가"","나"나""다"다""라"))))
y <- data.frame(x1 = 3:6)

bind_rows(x, y)
  x1   x2
1  1   가
2  2   나
3  3   다
4  4   라
5  3 <NA>
6  4 <NA>
7  5 <NA>
8  6 <NA>

Result! 데이터의 결측값은 NA로 처리된다.


# 열의 이름이 다른 두 데이터 프레임 결합NAx <- data.frame(x1 = 1:4, x2 = c("가"","나"나""다"다""라"))))
y <- data.frame(x1 = 3:6, x3 = c("a", "b", "c", "d"))

bind_rows(x, y)
  x1   x2   x3
1  1   가 <NA>
2  2   나 <NA>
3  3   다 <NA>
4  4   라 <NA>
5  3 <NA>    a
6  4 <NA>    b
7  5 <NA>    c
8  6 <NA>    d

Result! 데이터의 결측값은 NA로 처리된다.


5. 열을 대상으로 작업하는 함수

5-1. 함수 select()


5-1-1. 열 번호(또는 열 이름)에 의한 선택

# 열 번호data %>%
  select(c(1,4,7))
                     mpg  hp  qsec
Mazda RX4           21.0 110 16.46
Mazda RX4 Wag       21.0 110 17.02
Datsun 710          22.8  93 18.61
Hornet 4 Drive      21.4 110 19.44
Hornet Sportabout   18.7 175 17.02
Valiant             18.1 105 20.22
Duster 360          14.3 245 15.84
Merc 240D           24.4  62 20.00
Merc 230            22.8  95 22.90
Merc 280            19.2 123 18.30
Merc 280C           17.8 123 18.90
Merc 450SE          16.4 180 17.40
Merc 450SL          17.3 180 17.60
Merc 450SLC         15.2 180 18.00
Cadillac Fleetwood  10.4 205 17.98
Lincoln Continental 10.4 215 17.82
Chrysler Imperial   14.7 230 17.42
Fiat 128            32.4  66 19.47
Honda Civic         30.4  52 18.52
Toyota Corolla      33.9  65 19.90
Toyota Corona       21.5  97 20.01
Dodge Challenger    15.5 150 16.87
AMC Javelin         15.2 150 17.30
Camaro Z28          13.3 245 15.41
Pontiac Firebird    19.2 175 17.05
Fiat X1-9           27.3  66 18.90
Porsche 914-2       26.0  91 16.70
Lotus Europa        30.4 113 16.90
Ford Pantera L      15.8 264 14.50
Ferrari Dino        19.7 175 15.50
Maserati Bora       15.0 335 14.60
Volvo 142E          21.4 109 18.60

# 열 번호data %>%
  select(1:4)
                     mpg cyl  disp  hp
Mazda RX4           21.0   6 160.0 110
Mazda RX4 Wag       21.0   6 160.0 110
Datsun 710          22.8   4 108.0  93
Hornet 4 Drive      21.4   6 258.0 110
Hornet Sportabout   18.7   8 360.0 175
Valiant             18.1   6 225.0 105
Duster 360          14.3   8 360.0 245
Merc 240D           24.4   4 146.7  62
Merc 230            22.8   4 140.8  95
Merc 280            19.2   6 167.6 123
Merc 280C           17.8   6 167.6 123
Merc 450SE          16.4   8 275.8 180
Merc 450SL          17.3   8 275.8 180
Merc 450SLC         15.2   8 275.8 180
Cadillac Fleetwood  10.4   8 472.0 205
Lincoln Continental 10.4   8 460.0 215
Chrysler Imperial   14.7   8 440.0 230
Fiat 128            32.4   4  78.7  66
Honda Civic         30.4   4  75.7  52
Toyota Corolla      33.9   4  71.1  65
Toyota Corona       21.5   4 120.1  97
Dodge Challenger    15.5   8 318.0 150
AMC Javelin         15.2   8 304.0 150
Camaro Z28          13.3   8 350.0 245
Pontiac Firebird    19.2   8 400.0 175
Fiat X1-9           27.3   4  79.0  66
Porsche 914-2       26.0   4 120.3  91
Lotus Europa        30.4   4  95.1 113
Ford Pantera L      15.8   8 351.0 264
Ferrari Dino        19.7   6 145.0 175
Maserati Bora       15.0   8 301.0 335
Volvo 142E          21.4   4 121.0 109

# 열 이름data %>%
  select(c(mpg, cyl))
                     mpg cyl
Mazda RX4           21.0   6
Mazda RX4 Wag       21.0   6
Datsun 710          22.8   4
Hornet 4 Drive      21.4   6
Hornet Sportabout   18.7   8
Valiant             18.1   6
Duster 360          14.3   8
Merc 240D           24.4   4
Merc 230            22.8   4
Merc 280            19.2   6
Merc 280C           17.8   6
Merc 450SE          16.4   8
Merc 450SL          17.3   8
Merc 450SLC         15.2   8
Cadillac Fleetwood  10.4   8
Lincoln Continental 10.4   8
Chrysler Imperial   14.7   8
Fiat 128            32.4   4
Honda Civic         30.4   4
Toyota Corolla      33.9   4
Toyota Corona       21.5   4
Dodge Challenger    15.5   8
AMC Javelin         15.2   8
Camaro Z28          13.3   8
Pontiac Firebird    19.2   8
Fiat X1-9           27.3   4
Porsche 914-2       26.0   4
Lotus Europa        30.4   4
Ford Pantera L      15.8   8
Ferrari Dino        19.7   6
Maserati Bora       15.0   8
Volvo 142E          21.4   4

Caution! 열을 제거하고자 하는 경우에는 논리 부정 연산자(!) 또는 마이너스(-) 연산자를 사용해야 한다.

# 지정된 변수 제외
data %>%
  select(!c(mpg, cyl, hp))
                     disp drat    wt  qsec vs am gear carb
Mazda RX4           160.0 3.90 2.620 16.46  0  1    4    4
Mazda RX4 Wag       160.0 3.90 2.875 17.02  0  1    4    4
Datsun 710          108.0 3.85 2.320 18.61  1  1    4    1
Hornet 4 Drive      258.0 3.08 3.215 19.44  1  0    3    1
Hornet Sportabout   360.0 3.15 3.440 17.02  0  0    3    2
Valiant             225.0 2.76 3.460 20.22  1  0    3    1
Duster 360          360.0 3.21 3.570 15.84  0  0    3    4
Merc 240D           146.7 3.69 3.190 20.00  1  0    4    2
Merc 230            140.8 3.92 3.150 22.90  1  0    4    2
Merc 280            167.6 3.92 3.440 18.30  1  0    4    4
Merc 280C           167.6 3.92 3.440 18.90  1  0    4    4
Merc 450SE          275.8 3.07 4.070 17.40  0  0    3    3
Merc 450SL          275.8 3.07 3.730 17.60  0  0    3    3
Merc 450SLC         275.8 3.07 3.780 18.00  0  0    3    3
Cadillac Fleetwood  472.0 2.93 5.250 17.98  0  0    3    4
Lincoln Continental 460.0 3.00 5.424 17.82  0  0    3    4
Chrysler Imperial   440.0 3.23 5.345 17.42  0  0    3    4
Fiat 128             78.7 4.08 2.200 19.47  1  1    4    1
Honda Civic          75.7 4.93 1.615 18.52  1  1    4    2
Toyota Corolla       71.1 4.22 1.835 19.90  1  1    4    1
Toyota Corona       120.1 3.70 2.465 20.01  1  0    3    1
Dodge Challenger    318.0 2.76 3.520 16.87  0  0    3    2
AMC Javelin         304.0 3.15 3.435 17.30  0  0    3    2
Camaro Z28          350.0 3.73 3.840 15.41  0  0    3    4
Pontiac Firebird    400.0 3.08 3.845 17.05  0  0    3    2
Fiat X1-9            79.0 4.08 1.935 18.90  1  1    4    1
Porsche 914-2       120.3 4.43 2.140 16.70  0  1    5    2
Lotus Europa         95.1 3.77 1.513 16.90  1  1    5    2
Ford Pantera L      351.0 4.22 3.170 14.50  0  1    5    4
Ferrari Dino        145.0 3.62 2.770 15.50  0  1    5    6
Maserati Bora       301.0 3.54 3.570 14.60  0  1    5    8
Volvo 142E          121.0 4.11 2.780 18.60  1  1    4    2

# 지정된 변수 제외
data %>%
  select(-c(mpg, cyl, hp))
                     disp drat    wt  qsec vs am gear carb
Mazda RX4           160.0 3.90 2.620 16.46  0  1    4    4
Mazda RX4 Wag       160.0 3.90 2.875 17.02  0  1    4    4
Datsun 710          108.0 3.85 2.320 18.61  1  1    4    1
Hornet 4 Drive      258.0 3.08 3.215 19.44  1  0    3    1
Hornet Sportabout   360.0 3.15 3.440 17.02  0  0    3    2
Valiant             225.0 2.76 3.460 20.22  1  0    3    1
Duster 360          360.0 3.21 3.570 15.84  0  0    3    4
Merc 240D           146.7 3.69 3.190 20.00  1  0    4    2
Merc 230            140.8 3.92 3.150 22.90  1  0    4    2
Merc 280            167.6 3.92 3.440 18.30  1  0    4    4
Merc 280C           167.6 3.92 3.440 18.90  1  0    4    4
Merc 450SE          275.8 3.07 4.070 17.40  0  0    3    3
Merc 450SL          275.8 3.07 3.730 17.60  0  0    3    3
Merc 450SLC         275.8 3.07 3.780 18.00  0  0    3    3
Cadillac Fleetwood  472.0 2.93 5.250 17.98  0  0    3    4
Lincoln Continental 460.0 3.00 5.424 17.82  0  0    3    4
Chrysler Imperial   440.0 3.23 5.345 17.42  0  0    3    4
Fiat 128             78.7 4.08 2.200 19.47  1  1    4    1
Honda Civic          75.7 4.93 1.615 18.52  1  1    4    2
Toyota Corolla       71.1 4.22 1.835 19.90  1  1    4    1
Toyota Corona       120.1 3.70 2.465 20.01  1  0    3    1
Dodge Challenger    318.0 2.76 3.520 16.87  0  0    3    2
AMC Javelin         304.0 3.15 3.435 17.30  0  0    3    2
Camaro Z28          350.0 3.73 3.840 15.41  0  0    3    4
Pontiac Firebird    400.0 3.08 3.845 17.05  0  0    3    2
Fiat X1-9            79.0 4.08 1.935 18.90  1  1    4    1
Porsche 914-2       120.3 4.43 2.140 16.70  0  1    5    2
Lotus Europa         95.1 3.77 1.513 16.90  1  1    5    2
Ford Pantera L      351.0 4.22 3.170 14.50  0  1    5    4
Ferrari Dino        145.0 3.62 2.770 15.50  0  1    5    6
Maserati Bora       301.0 3.54 3.570 14.60  0  1    5    8
Volvo 142E          121.0 4.11 2.780 18.60  1  1    4    2

# 열 선택과 제거data %>%
  select(1:4, -(8:10))
                     mpg cyl  disp  hp
Mazda RX4           21.0   6 160.0 110
Mazda RX4 Wag       21.0   6 160.0 110
Datsun 710          22.8   4 108.0  93
Hornet 4 Drive      21.4   6 258.0 110
Hornet Sportabout   18.7   8 360.0 175
Valiant             18.1   6 225.0 105
Duster 360          14.3   8 360.0 245
Merc 240D           24.4   4 146.7  62
Merc 230            22.8   4 140.8  95
Merc 280            19.2   6 167.6 123
Merc 280C           17.8   6 167.6 123
Merc 450SE          16.4   8 275.8 180
Merc 450SL          17.3   8 275.8 180
Merc 450SLC         15.2   8 275.8 180
Cadillac Fleetwood  10.4   8 472.0 205
Lincoln Continental 10.4   8 460.0 215
Chrysler Imperial   14.7   8 440.0 230
Fiat 128            32.4   4  78.7  66
Honda Civic         30.4   4  75.7  52
Toyota Corolla      33.9   4  71.1  65
Toyota Corona       21.5   4 120.1  97
Dodge Challenger    15.5   8 318.0 150
AMC Javelin         15.2   8 304.0 150
Camaro Z28          13.3   8 350.0 245
Pontiac Firebird    19.2   8 400.0 175
Fiat X1-9           27.3   4  79.0  66
Porsche 914-2       26.0   4 120.3  91
Lotus Europa        30.4   4  95.1 113
Ford Pantera L      15.8   8 351.0 264
Ferrari Dino        19.7   6 145.0 175
Maserati Bora       15.0   8 301.0 335
Volvo 142E          21.4   4 121.0 109

5-1-2. 변수 유형에 의한 선택

# 숫자형 변수만 선택
data %>%
  select(where(is.numeric))
                     mpg cyl  disp  hp drat    wt  qsec vs am gear
Mazda RX4           21.0   6 160.0 110 3.90 2.620 16.46  0  1    4
Mazda RX4 Wag       21.0   6 160.0 110 3.90 2.875 17.02  0  1    4
Datsun 710          22.8   4 108.0  93 3.85 2.320 18.61  1  1    4
Hornet 4 Drive      21.4   6 258.0 110 3.08 3.215 19.44  1  0    3
Hornet Sportabout   18.7   8 360.0 175 3.15 3.440 17.02  0  0    3
Valiant             18.1   6 225.0 105 2.76 3.460 20.22  1  0    3
Duster 360          14.3   8 360.0 245 3.21 3.570 15.84  0  0    3
Merc 240D           24.4   4 146.7  62 3.69 3.190 20.00  1  0    4
Merc 230            22.8   4 140.8  95 3.92 3.150 22.90  1  0    4
Merc 280            19.2   6 167.6 123 3.92 3.440 18.30  1  0    4
Merc 280C           17.8   6 167.6 123 3.92 3.440 18.90  1  0    4
Merc 450SE          16.4   8 275.8 180 3.07 4.070 17.40  0  0    3
Merc 450SL          17.3   8 275.8 180 3.07 3.730 17.60  0  0    3
Merc 450SLC         15.2   8 275.8 180 3.07 3.780 18.00  0  0    3
Cadillac Fleetwood  10.4   8 472.0 205 2.93 5.250 17.98  0  0    3
Lincoln Continental 10.4   8 460.0 215 3.00 5.424 17.82  0  0    3
Chrysler Imperial   14.7   8 440.0 230 3.23 5.345 17.42  0  0    3
Fiat 128            32.4   4  78.7  66 4.08 2.200 19.47  1  1    4
Honda Civic         30.4   4  75.7  52 4.93 1.615 18.52  1  1    4
Toyota Corolla      33.9   4  71.1  65 4.22 1.835 19.90  1  1    4
Toyota Corona       21.5   4 120.1  97 3.70 2.465 20.01  1  0    3
Dodge Challenger    15.5   8 318.0 150 2.76 3.520 16.87  0  0    3
AMC Javelin         15.2   8 304.0 150 3.15 3.435 17.30  0  0    3
Camaro Z28          13.3   8 350.0 245 3.73 3.840 15.41  0  0    3
Pontiac Firebird    19.2   8 400.0 175 3.08 3.845 17.05  0  0    3
Fiat X1-9           27.3   4  79.0  66 4.08 1.935 18.90  1  1    4
Porsche 914-2       26.0   4 120.3  91 4.43 2.140 16.70  0  1    5
Lotus Europa        30.4   4  95.1 113 3.77 1.513 16.90  1  1    5
Ford Pantera L      15.8   8 351.0 264 4.22 3.170 14.50  0  1    5
Ferrari Dino        19.7   6 145.0 175 3.62 2.770 15.50  0  1    5
Maserati Bora       15.0   8 301.0 335 3.54 3.570 14.60  0  1    5
Volvo 142E          21.4   4 121.0 109 4.11 2.780 18.60  1  1    4
                    carb
Mazda RX4              4
Mazda RX4 Wag          4
Datsun 710             1
Hornet 4 Drive         1
Hornet Sportabout      2
Valiant                1
Duster 360             4
Merc 240D              2
Merc 230               2
Merc 280               4
Merc 280C              4
Merc 450SE             3
Merc 450SL             3
Merc 450SLC            3
Cadillac Fleetwood     4
Lincoln Continental    4
Chrysler Imperial      4
Fiat 128               1
Honda Civic            2
Toyota Corolla         1
Toyota Corona          1
Dodge Challenger       2
AMC Javelin            2
Camaro Z28             4
Pontiac Firebird       2
Fiat X1-9              1
Porsche 914-2          2
Lotus Europa           2
Ford Pantera L         4
Ferrari Dino           6
Maserati Bora          8
Volvo 142E             2

# 숫자형 변수이거나 문자형 변수 선택data %>%
  select(where(is.numeric) |  where(is.character))
                     mpg cyl  disp  hp drat    wt  qsec vs am gear
Mazda RX4           21.0   6 160.0 110 3.90 2.620 16.46  0  1    4
Mazda RX4 Wag       21.0   6 160.0 110 3.90 2.875 17.02  0  1    4
Datsun 710          22.8   4 108.0  93 3.85 2.320 18.61  1  1    4
Hornet 4 Drive      21.4   6 258.0 110 3.08 3.215 19.44  1  0    3
Hornet Sportabout   18.7   8 360.0 175 3.15 3.440 17.02  0  0    3
Valiant             18.1   6 225.0 105 2.76 3.460 20.22  1  0    3
Duster 360          14.3   8 360.0 245 3.21 3.570 15.84  0  0    3
Merc 240D           24.4   4 146.7  62 3.69 3.190 20.00  1  0    4
Merc 230            22.8   4 140.8  95 3.92 3.150 22.90  1  0    4
Merc 280            19.2   6 167.6 123 3.92 3.440 18.30  1  0    4
Merc 280C           17.8   6 167.6 123 3.92 3.440 18.90  1  0    4
Merc 450SE          16.4   8 275.8 180 3.07 4.070 17.40  0  0    3
Merc 450SL          17.3   8 275.8 180 3.07 3.730 17.60  0  0    3
Merc 450SLC         15.2   8 275.8 180 3.07 3.780 18.00  0  0    3
Cadillac Fleetwood  10.4   8 472.0 205 2.93 5.250 17.98  0  0    3
Lincoln Continental 10.4   8 460.0 215 3.00 5.424 17.82  0  0    3
Chrysler Imperial   14.7   8 440.0 230 3.23 5.345 17.42  0  0    3
Fiat 128            32.4   4  78.7  66 4.08 2.200 19.47  1  1    4
Honda Civic         30.4   4  75.7  52 4.93 1.615 18.52  1  1    4
Toyota Corolla      33.9   4  71.1  65 4.22 1.835 19.90  1  1    4
Toyota Corona       21.5   4 120.1  97 3.70 2.465 20.01  1  0    3
Dodge Challenger    15.5   8 318.0 150 2.76 3.520 16.87  0  0    3
AMC Javelin         15.2   8 304.0 150 3.15 3.435 17.30  0  0    3
Camaro Z28          13.3   8 350.0 245 3.73 3.840 15.41  0  0    3
Pontiac Firebird    19.2   8 400.0 175 3.08 3.845 17.05  0  0    3
Fiat X1-9           27.3   4  79.0  66 4.08 1.935 18.90  1  1    4
Porsche 914-2       26.0   4 120.3  91 4.43 2.140 16.70  0  1    5
Lotus Europa        30.4   4  95.1 113 3.77 1.513 16.90  1  1    5
Ford Pantera L      15.8   8 351.0 264 4.22 3.170 14.50  0  1    5
Ferrari Dino        19.7   6 145.0 175 3.62 2.770 15.50  0  1    5
Maserati Bora       15.0   8 301.0 335 3.54 3.570 14.60  0  1    5
Volvo 142E          21.4   4 121.0 109 4.11 2.780 18.60  1  1    4
                    carb
Mazda RX4              4
Mazda RX4 Wag          4
Datsun 710             1
Hornet 4 Drive         1
Hornet Sportabout      2
Valiant                1
Duster 360             4
Merc 240D              2
Merc 230               2
Merc 280               4
Merc 280C              4
Merc 450SE             3
Merc 450SL             3
Merc 450SLC            3
Cadillac Fleetwood     4
Lincoln Continental    4
Chrysler Imperial      4
Fiat 128               1
Honda Civic            2
Toyota Corolla         1
Toyota Corona          1
Dodge Challenger       2
AMC Javelin            2
Camaro Z28             4
Pontiac Firebird       2
Fiat X1-9              1
Porsche 914-2          2
Lotus Europa           2
Ford Pantera L         4
Ferrari Dino           6
Maserati Bora          8
Volvo 142E             2

5-1-3. 변수 선택과 관련된 함수의 이용

특정 위치의 열을 선택할 때 사용할 수 있는 함수
everythig() 모든 변수 선택
last_col() 마지막 변수 선택
변수 이름을 구성하고 있는 문자열에 대한 매칭 작업으로 변수를 선택할 때 사용할 수 있는 함수
starts_with(“x”) 이름이 “x”로 시작하는 변수 선택
ends_with(“x”) 이름이 “x”로 끝나는 변수 선택
contains(“x”) 이름에 “x”가 포함된 변수 선택
# 모든 변수 선택data %>%
  select(everything())
                     mpg cyl  disp  hp drat    wt  qsec vs am gear
Mazda RX4           21.0   6 160.0 110 3.90 2.620 16.46  0  1    4
Mazda RX4 Wag       21.0   6 160.0 110 3.90 2.875 17.02  0  1    4
Datsun 710          22.8   4 108.0  93 3.85 2.320 18.61  1  1    4
Hornet 4 Drive      21.4   6 258.0 110 3.08 3.215 19.44  1  0    3
Hornet Sportabout   18.7   8 360.0 175 3.15 3.440 17.02  0  0    3
Valiant             18.1   6 225.0 105 2.76 3.460 20.22  1  0    3
Duster 360          14.3   8 360.0 245 3.21 3.570 15.84  0  0    3
Merc 240D           24.4   4 146.7  62 3.69 3.190 20.00  1  0    4
Merc 230            22.8   4 140.8  95 3.92 3.150 22.90  1  0    4
Merc 280            19.2   6 167.6 123 3.92 3.440 18.30  1  0    4
Merc 280C           17.8   6 167.6 123 3.92 3.440 18.90  1  0    4
Merc 450SE          16.4   8 275.8 180 3.07 4.070 17.40  0  0    3
Merc 450SL          17.3   8 275.8 180 3.07 3.730 17.60  0  0    3
Merc 450SLC         15.2   8 275.8 180 3.07 3.780 18.00  0  0    3
Cadillac Fleetwood  10.4   8 472.0 205 2.93 5.250 17.98  0  0    3
Lincoln Continental 10.4   8 460.0 215 3.00 5.424 17.82  0  0    3
Chrysler Imperial   14.7   8 440.0 230 3.23 5.345 17.42  0  0    3
Fiat 128            32.4   4  78.7  66 4.08 2.200 19.47  1  1    4
Honda Civic         30.4   4  75.7  52 4.93 1.615 18.52  1  1    4
Toyota Corolla      33.9   4  71.1  65 4.22 1.835 19.90  1  1    4
Toyota Corona       21.5   4 120.1  97 3.70 2.465 20.01  1  0    3
Dodge Challenger    15.5   8 318.0 150 2.76 3.520 16.87  0  0    3
AMC Javelin         15.2   8 304.0 150 3.15 3.435 17.30  0  0    3
Camaro Z28          13.3   8 350.0 245 3.73 3.840 15.41  0  0    3
Pontiac Firebird    19.2   8 400.0 175 3.08 3.845 17.05  0  0    3
Fiat X1-9           27.3   4  79.0  66 4.08 1.935 18.90  1  1    4
Porsche 914-2       26.0   4 120.3  91 4.43 2.140 16.70  0  1    5
Lotus Europa        30.4   4  95.1 113 3.77 1.513 16.90  1  1    5
Ford Pantera L      15.8   8 351.0 264 4.22 3.170 14.50  0  1    5
Ferrari Dino        19.7   6 145.0 175 3.62 2.770 15.50  0  1    5
Maserati Bora       15.0   8 301.0 335 3.54 3.570 14.60  0  1    5
Volvo 142E          21.4   4 121.0 109 4.11 2.780 18.60  1  1    4
                    carb
Mazda RX4              4
Mazda RX4 Wag          4
Datsun 710             1
Hornet 4 Drive         1
Hornet Sportabout      2
Valiant                1
Duster 360             4
Merc 240D              2
Merc 230               2
Merc 280               4
Merc 280C              4
Merc 450SE             3
Merc 450SL             3
Merc 450SLC            3
Cadillac Fleetwood     4
Lincoln Continental    4
Chrysler Imperial      4
Fiat 128               1
Honda Civic            2
Toyota Corolla         1
Toyota Corona          1
Dodge Challenger       2
AMC Javelin            2
Camaro Z28             4
Pontiac Firebird       2
Fiat X1-9              1
Porsche 914-2          2
Lotus Europa           2
Ford Pantera L         4
Ferrari Dino           6
Maserati Bora          8
Volvo 142E             2

# 마지막 변수 선택선택
data %>%
  select(last_col())
                    carb
Mazda RX4              4
Mazda RX4 Wag          4
Datsun 710             1
Hornet 4 Drive         1
Hornet Sportabout      2
Valiant                1
Duster 360             4
Merc 240D              2
Merc 230               2
Merc 280               4
Merc 280C              4
Merc 450SE             3
Merc 450SL             3
Merc 450SLC            3
Cadillac Fleetwood     4
Lincoln Continental    4
Chrysler Imperial      4
Fiat 128               1
Honda Civic            2
Toyota Corolla         1
Toyota Corona          1
Dodge Challenger       2
AMC Javelin            2
Camaro Z28             4
Pontiac Firebird       2
Fiat X1-9              1
Porsche 914-2          2
Lotus Europa           2
Ford Pantera L         4
Ferrari Dino           6
Maserati Bora          8
Volvo 142E             2

# 변수 이름이 m으로 시작하는 변수 선택data %>%
  select(starts_with("m"))
                     mpg
Mazda RX4           21.0
Mazda RX4 Wag       21.0
Datsun 710          22.8
Hornet 4 Drive      21.4
Hornet Sportabout   18.7
Valiant             18.1
Duster 360          14.3
Merc 240D           24.4
Merc 230            22.8
Merc 280            19.2
Merc 280C           17.8
Merc 450SE          16.4
Merc 450SL          17.3
Merc 450SLC         15.2
Cadillac Fleetwood  10.4
Lincoln Continental 10.4
Chrysler Imperial   14.7
Fiat 128            32.4
Honda Civic         30.4
Toyota Corolla      33.9
Toyota Corona       21.5
Dodge Challenger    15.5
AMC Javelin         15.2
Camaro Z28          13.3
Pontiac Firebird    19.2
Fiat X1-9           27.3
Porsche 914-2       26.0
Lotus Europa        30.4
Ford Pantera L      15.8
Ferrari Dino        19.7
Maserati Bora       15.0
Volvo 142E          21.4

Caution! 대소문자를 구분하기 위해 옵션 .ignore.case = FALSE를 해야 한다.


# 변수 이름이 p으로 끝나는 변수 선택 선택
data %>%
  select(ends_with("p"))
                     disp  hp
Mazda RX4           160.0 110
Mazda RX4 Wag       160.0 110
Datsun 710          108.0  93
Hornet 4 Drive      258.0 110
Hornet Sportabout   360.0 175
Valiant             225.0 105
Duster 360          360.0 245
Merc 240D           146.7  62
Merc 230            140.8  95
Merc 280            167.6 123
Merc 280C           167.6 123
Merc 450SE          275.8 180
Merc 450SL          275.8 180
Merc 450SLC         275.8 180
Cadillac Fleetwood  472.0 205
Lincoln Continental 460.0 215
Chrysler Imperial   440.0 230
Fiat 128             78.7  66
Honda Civic          75.7  52
Toyota Corolla       71.1  65
Toyota Corona       120.1  97
Dodge Challenger    318.0 150
AMC Javelin         304.0 150
Camaro Z28          350.0 245
Pontiac Firebird    400.0 175
Fiat X1-9            79.0  66
Porsche 914-2       120.3  91
Lotus Europa         95.1 113
Ford Pantera L      351.0 264
Ferrari Dino        145.0 175
Maserati Bora       301.0 335
Volvo 142E          121.0 109

Caution! 대소문자를 구분하기 위해 옵션 .ignore.case = FALSE를 해야 한다.


# 변수 이름에 A가 포함되어 있는 변수 선택data %>%
  select(contains("A"))
                    drat am gear carb
Mazda RX4           3.90  1    4    4
Mazda RX4 Wag       3.90  1    4    4
Datsun 710          3.85  1    4    1
Hornet 4 Drive      3.08  0    3    1
Hornet Sportabout   3.15  0    3    2
Valiant             2.76  0    3    1
Duster 360          3.21  0    3    4
Merc 240D           3.69  0    4    2
Merc 230            3.92  0    4    2
Merc 280            3.92  0    4    4
Merc 280C           3.92  0    4    4
Merc 450SE          3.07  0    3    3
Merc 450SL          3.07  0    3    3
Merc 450SLC         3.07  0    3    3
Cadillac Fleetwood  2.93  0    3    4
Lincoln Continental 3.00  0    3    4
Chrysler Imperial   3.23  0    3    4
Fiat 128            4.08  1    4    1
Honda Civic         4.93  1    4    2
Toyota Corolla      4.22  1    4    1
Toyota Corona       3.70  0    3    1
Dodge Challenger    2.76  0    3    2
AMC Javelin         3.15  0    3    2
Camaro Z28          3.73  0    3    4
Pontiac Firebird    3.08  0    3    2
Fiat X1-9           4.08  1    4    1
Porsche 914-2       4.43  1    5    2
Lotus Europa        3.77  1    5    2
Ford Pantera L      4.22  1    5    4
Ferrari Dino        3.62  1    5    6
Maserati Bora       3.54  1    5    8
Volvo 142E          4.11  1    4    2

Caution! 대소문자를 구분하기 위해 옵션 .ignore.case = FALSE를 해야 한다.


5-2. 함수 pull()

data %>%
  pull(var = am)
 [1] 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 1 1 1 1 1 1 1

data %>%
  pull(var = 3)
 [1] 160.0 160.0 108.0 258.0 360.0 225.0 360.0 146.7 140.8 167.6 167.6
[12] 275.8 275.8 275.8 472.0 460.0 440.0  78.7  75.7  71.1 120.1 318.0
[23] 304.0 350.0 400.0  79.0 120.3  95.1 351.0 145.0 301.0 121.0

Result! 앞에서 세 번째 변수인 “disp”의 값들이 추출되었다.


data %>%
  pull(var = -3)
 [1] 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 1 1 1 1 1 1 1

Result! 뒤에서 세 번째 변수인 “am”의 값들이 추출되었다.


5-3. 함수 rename()

data %>%
  rename(MPG = mpg)
                     MPG cyl  disp  hp drat    wt  qsec vs am gear
Mazda RX4           21.0   6 160.0 110 3.90 2.620 16.46  0  1    4
Mazda RX4 Wag       21.0   6 160.0 110 3.90 2.875 17.02  0  1    4
Datsun 710          22.8   4 108.0  93 3.85 2.320 18.61  1  1    4
Hornet 4 Drive      21.4   6 258.0 110 3.08 3.215 19.44  1  0    3
Hornet Sportabout   18.7   8 360.0 175 3.15 3.440 17.02  0  0    3
Valiant             18.1   6 225.0 105 2.76 3.460 20.22  1  0    3
Duster 360          14.3   8 360.0 245 3.21 3.570 15.84  0  0    3
Merc 240D           24.4   4 146.7  62 3.69 3.190 20.00  1  0    4
Merc 230            22.8   4 140.8  95 3.92 3.150 22.90  1  0    4
Merc 280            19.2   6 167.6 123 3.92 3.440 18.30  1  0    4
Merc 280C           17.8   6 167.6 123 3.92 3.440 18.90  1  0    4
Merc 450SE          16.4   8 275.8 180 3.07 4.070 17.40  0  0    3
Merc 450SL          17.3   8 275.8 180 3.07 3.730 17.60  0  0    3
Merc 450SLC         15.2   8 275.8 180 3.07 3.780 18.00  0  0    3
Cadillac Fleetwood  10.4   8 472.0 205 2.93 5.250 17.98  0  0    3
Lincoln Continental 10.4   8 460.0 215 3.00 5.424 17.82  0  0    3
Chrysler Imperial   14.7   8 440.0 230 3.23 5.345 17.42  0  0    3
Fiat 128            32.4   4  78.7  66 4.08 2.200 19.47  1  1    4
Honda Civic         30.4   4  75.7  52 4.93 1.615 18.52  1  1    4
Toyota Corolla      33.9   4  71.1  65 4.22 1.835 19.90  1  1    4
Toyota Corona       21.5   4 120.1  97 3.70 2.465 20.01  1  0    3
Dodge Challenger    15.5   8 318.0 150 2.76 3.520 16.87  0  0    3
AMC Javelin         15.2   8 304.0 150 3.15 3.435 17.30  0  0    3
Camaro Z28          13.3   8 350.0 245 3.73 3.840 15.41  0  0    3
Pontiac Firebird    19.2   8 400.0 175 3.08 3.845 17.05  0  0    3
Fiat X1-9           27.3   4  79.0  66 4.08 1.935 18.90  1  1    4
Porsche 914-2       26.0   4 120.3  91 4.43 2.140 16.70  0  1    5
Lotus Europa        30.4   4  95.1 113 3.77 1.513 16.90  1  1    5
Ford Pantera L      15.8   8 351.0 264 4.22 3.170 14.50  0  1    5
Ferrari Dino        19.7   6 145.0 175 3.62 2.770 15.50  0  1    5
Maserati Bora       15.0   8 301.0 335 3.54 3.570 14.60  0  1    5
Volvo 142E          21.4   4 121.0 109 4.11 2.780 18.60  1  1    4
                    carb
Mazda RX4              4
Mazda RX4 Wag          4
Datsun 710             1
Hornet 4 Drive         1
Hornet Sportabout      2
Valiant                1
Duster 360             4
Merc 240D              2
Merc 230               2
Merc 280               4
Merc 280C              4
Merc 450SE             3
Merc 450SL             3
Merc 450SLC            3
Cadillac Fleetwood     4
Lincoln Continental    4
Chrysler Imperial      4
Fiat 128               1
Honda Civic            2
Toyota Corolla         1
Toyota Corona          1
Dodge Challenger       2
AMC Javelin            2
Camaro Z28             4
Pontiac Firebird       2
Fiat X1-9              1
Porsche 914-2          2
Lotus Europa           2
Ford Pantera L         4
Ferrari Dino           6
Maserati Bora          8
Volvo 142E             2

Caution! 많은 변수의 이름을 어떤 공통된 양식에 따라 모두 바꿔야 하는 경우, 하나씩 변경하는 방식은 매우 비효율적이다. 예를 들어, 대문자로 입력된 변수 이름을 모두 소문자로 변경하거나, 이름 중간에 들어간 점(.)을 밑줄(_)로 변경해야하는 경우에 하나씩 변경하는 방식이 아닌 해당되는 변수의 이름을 한꺼번에 모두 바꿀 수 있는 방식이 필요하다. 이러한 작업은 함수 rename_with()로 할 수 있다.

# 모든 변수 이름을 대문자로 변경변경
data %>%
  rename_with(toupper)
                     MPG CYL  DISP  HP DRAT    WT  QSEC VS AM GEAR
Mazda RX4           21.0   6 160.0 110 3.90 2.620 16.46  0  1    4
Mazda RX4 Wag       21.0   6 160.0 110 3.90 2.875 17.02  0  1    4
Datsun 710          22.8   4 108.0  93 3.85 2.320 18.61  1  1    4
Hornet 4 Drive      21.4   6 258.0 110 3.08 3.215 19.44  1  0    3
Hornet Sportabout   18.7   8 360.0 175 3.15 3.440 17.02  0  0    3
Valiant             18.1   6 225.0 105 2.76 3.460 20.22  1  0    3
Duster 360          14.3   8 360.0 245 3.21 3.570 15.84  0  0    3
Merc 240D           24.4   4 146.7  62 3.69 3.190 20.00  1  0    4
Merc 230            22.8   4 140.8  95 3.92 3.150 22.90  1  0    4
Merc 280            19.2   6 167.6 123 3.92 3.440 18.30  1  0    4
Merc 280C           17.8   6 167.6 123 3.92 3.440 18.90  1  0    4
Merc 450SE          16.4   8 275.8 180 3.07 4.070 17.40  0  0    3
Merc 450SL          17.3   8 275.8 180 3.07 3.730 17.60  0  0    3
Merc 450SLC         15.2   8 275.8 180 3.07 3.780 18.00  0  0    3
Cadillac Fleetwood  10.4   8 472.0 205 2.93 5.250 17.98  0  0    3
Lincoln Continental 10.4   8 460.0 215 3.00 5.424 17.82  0  0    3
Chrysler Imperial   14.7   8 440.0 230 3.23 5.345 17.42  0  0    3
Fiat 128            32.4   4  78.7  66 4.08 2.200 19.47  1  1    4
Honda Civic         30.4   4  75.7  52 4.93 1.615 18.52  1  1    4
Toyota Corolla      33.9   4  71.1  65 4.22 1.835 19.90  1  1    4
Toyota Corona       21.5   4 120.1  97 3.70 2.465 20.01  1  0    3
Dodge Challenger    15.5   8 318.0 150 2.76 3.520 16.87  0  0    3
AMC Javelin         15.2   8 304.0 150 3.15 3.435 17.30  0  0    3
Camaro Z28          13.3   8 350.0 245 3.73 3.840 15.41  0  0    3
Pontiac Firebird    19.2   8 400.0 175 3.08 3.845 17.05  0  0    3
Fiat X1-9           27.3   4  79.0  66 4.08 1.935 18.90  1  1    4
Porsche 914-2       26.0   4 120.3  91 4.43 2.140 16.70  0  1    5
Lotus Europa        30.4   4  95.1 113 3.77 1.513 16.90  1  1    5
Ford Pantera L      15.8   8 351.0 264 4.22 3.170 14.50  0  1    5
Ferrari Dino        19.7   6 145.0 175 3.62 2.770 15.50  0  1    5
Maserati Bora       15.0   8 301.0 335 3.54 3.570 14.60  0  1    5
Volvo 142E          21.4   4 121.0 109 4.11 2.780 18.60  1  1    4
                    CARB
Mazda RX4              4
Mazda RX4 Wag          4
Datsun 710             1
Hornet 4 Drive         1
Hornet Sportabout      2
Valiant                1
Duster 360             4
Merc 240D              2
Merc 230               2
Merc 280               4
Merc 280C              4
Merc 450SE             3
Merc 450SL             3
Merc 450SLC            3
Cadillac Fleetwood     4
Lincoln Continental    4
Chrysler Imperial      4
Fiat 128               1
Honda Civic            2
Toyota Corolla         1
Toyota Corona          1
Dodge Challenger       2
AMC Javelin            2
Camaro Z28             4
Pontiac Firebird       2
Fiat X1-9              1
Porsche 914-2          2
Lotus Europa           2
Ford Pantera L         4
Ferrari Dino           6
Maserati Bora          8
Volvo 142E             2

# 변수 이름에 a가 포함되어 있는 변수만 대문자로 변경 변경
data %>%
  rename_with(toupper, contains("a"))
                     mpg cyl  disp  hp DRAT    wt  qsec vs AM GEAR
Mazda RX4           21.0   6 160.0 110 3.90 2.620 16.46  0  1    4
Mazda RX4 Wag       21.0   6 160.0 110 3.90 2.875 17.02  0  1    4
Datsun 710          22.8   4 108.0  93 3.85 2.320 18.61  1  1    4
Hornet 4 Drive      21.4   6 258.0 110 3.08 3.215 19.44  1  0    3
Hornet Sportabout   18.7   8 360.0 175 3.15 3.440 17.02  0  0    3
Valiant             18.1   6 225.0 105 2.76 3.460 20.22  1  0    3
Duster 360          14.3   8 360.0 245 3.21 3.570 15.84  0  0    3
Merc 240D           24.4   4 146.7  62 3.69 3.190 20.00  1  0    4
Merc 230            22.8   4 140.8  95 3.92 3.150 22.90  1  0    4
Merc 280            19.2   6 167.6 123 3.92 3.440 18.30  1  0    4
Merc 280C           17.8   6 167.6 123 3.92 3.440 18.90  1  0    4
Merc 450SE          16.4   8 275.8 180 3.07 4.070 17.40  0  0    3
Merc 450SL          17.3   8 275.8 180 3.07 3.730 17.60  0  0    3
Merc 450SLC         15.2   8 275.8 180 3.07 3.780 18.00  0  0    3
Cadillac Fleetwood  10.4   8 472.0 205 2.93 5.250 17.98  0  0    3
Lincoln Continental 10.4   8 460.0 215 3.00 5.424 17.82  0  0    3
Chrysler Imperial   14.7   8 440.0 230 3.23 5.345 17.42  0  0    3
Fiat 128            32.4   4  78.7  66 4.08 2.200 19.47  1  1    4
Honda Civic         30.4   4  75.7  52 4.93 1.615 18.52  1  1    4
Toyota Corolla      33.9   4  71.1  65 4.22 1.835 19.90  1  1    4
Toyota Corona       21.5   4 120.1  97 3.70 2.465 20.01  1  0    3
Dodge Challenger    15.5   8 318.0 150 2.76 3.520 16.87  0  0    3
AMC Javelin         15.2   8 304.0 150 3.15 3.435 17.30  0  0    3
Camaro Z28          13.3   8 350.0 245 3.73 3.840 15.41  0  0    3
Pontiac Firebird    19.2   8 400.0 175 3.08 3.845 17.05  0  0    3
Fiat X1-9           27.3   4  79.0  66 4.08 1.935 18.90  1  1    4
Porsche 914-2       26.0   4 120.3  91 4.43 2.140 16.70  0  1    5
Lotus Europa        30.4   4  95.1 113 3.77 1.513 16.90  1  1    5
Ford Pantera L      15.8   8 351.0 264 4.22 3.170 14.50  0  1    5
Ferrari Dino        19.7   6 145.0 175 3.62 2.770 15.50  0  1    5
Maserati Bora       15.0   8 301.0 335 3.54 3.570 14.60  0  1    5
Volvo 142E          21.4   4 121.0 109 4.11 2.780 18.60  1  1    4
                    CARB
Mazda RX4              4
Mazda RX4 Wag          4
Datsun 710             1
Hornet 4 Drive         1
Hornet Sportabout      2
Valiant                1
Duster 360             4
Merc 240D              2
Merc 230               2
Merc 280               4
Merc 280C              4
Merc 450SE             3
Merc 450SL             3
Merc 450SLC            3
Cadillac Fleetwood     4
Lincoln Continental    4
Chrysler Imperial      4
Fiat 128               1
Honda Civic            2
Toyota Corolla         1
Toyota Corona          1
Dodge Challenger       2
AMC Javelin            2
Camaro Z28             4
Pontiac Firebird       2
Fiat X1-9              1
Porsche 914-2          2
Lotus Europa           2
Ford Pantera L         4
Ferrari Dino           6
Maserati Bora          8
Volvo 142E             2

5-4. 함수 relocate()

# 변수 wt를 제일 앞으로 이동data %>%
  relocate(wt)
                       wt  mpg cyl  disp  hp drat  qsec vs am gear
Mazda RX4           2.620 21.0   6 160.0 110 3.90 16.46  0  1    4
Mazda RX4 Wag       2.875 21.0   6 160.0 110 3.90 17.02  0  1    4
Datsun 710          2.320 22.8   4 108.0  93 3.85 18.61  1  1    4
Hornet 4 Drive      3.215 21.4   6 258.0 110 3.08 19.44  1  0    3
Hornet Sportabout   3.440 18.7   8 360.0 175 3.15 17.02  0  0    3
Valiant             3.460 18.1   6 225.0 105 2.76 20.22  1  0    3
Duster 360          3.570 14.3   8 360.0 245 3.21 15.84  0  0    3
Merc 240D           3.190 24.4   4 146.7  62 3.69 20.00  1  0    4
Merc 230            3.150 22.8   4 140.8  95 3.92 22.90  1  0    4
Merc 280            3.440 19.2   6 167.6 123 3.92 18.30  1  0    4
Merc 280C           3.440 17.8   6 167.6 123 3.92 18.90  1  0    4
Merc 450SE          4.070 16.4   8 275.8 180 3.07 17.40  0  0    3
Merc 450SL          3.730 17.3   8 275.8 180 3.07 17.60  0  0    3
Merc 450SLC         3.780 15.2   8 275.8 180 3.07 18.00  0  0    3
Cadillac Fleetwood  5.250 10.4   8 472.0 205 2.93 17.98  0  0    3
Lincoln Continental 5.424 10.4   8 460.0 215 3.00 17.82  0  0    3
Chrysler Imperial   5.345 14.7   8 440.0 230 3.23 17.42  0  0    3
Fiat 128            2.200 32.4   4  78.7  66 4.08 19.47  1  1    4
Honda Civic         1.615 30.4   4  75.7  52 4.93 18.52  1  1    4
Toyota Corolla      1.835 33.9   4  71.1  65 4.22 19.90  1  1    4
Toyota Corona       2.465 21.5   4 120.1  97 3.70 20.01  1  0    3
Dodge Challenger    3.520 15.5   8 318.0 150 2.76 16.87  0  0    3
AMC Javelin         3.435 15.2   8 304.0 150 3.15 17.30  0  0    3
Camaro Z28          3.840 13.3   8 350.0 245 3.73 15.41  0  0    3
Pontiac Firebird    3.845 19.2   8 400.0 175 3.08 17.05  0  0    3
Fiat X1-9           1.935 27.3   4  79.0  66 4.08 18.90  1  1    4
Porsche 914-2       2.140 26.0   4 120.3  91 4.43 16.70  0  1    5
Lotus Europa        1.513 30.4   4  95.1 113 3.77 16.90  1  1    5
Ford Pantera L      3.170 15.8   8 351.0 264 4.22 14.50  0  1    5
Ferrari Dino        2.770 19.7   6 145.0 175 3.62 15.50  0  1    5
Maserati Bora       3.570 15.0   8 301.0 335 3.54 14.60  0  1    5
Volvo 142E          2.780 21.4   4 121.0 109 4.11 18.60  1  1    4
                    carb
Mazda RX4              4
Mazda RX4 Wag          4
Datsun 710             1
Hornet 4 Drive         1
Hornet Sportabout      2
Valiant                1
Duster 360             4
Merc 240D              2
Merc 230               2
Merc 280               4
Merc 280C              4
Merc 450SE             3
Merc 450SL             3
Merc 450SLC            3
Cadillac Fleetwood     4
Lincoln Continental    4
Chrysler Imperial      4
Fiat 128               1
Honda Civic            2
Toyota Corolla         1
Toyota Corona          1
Dodge Challenger       2
AMC Javelin            2
Camaro Z28             4
Pontiac Firebird       2
Fiat X1-9              1
Porsche 914-2          2
Lotus Europa           2
Ford Pantera L         4
Ferrari Dino           6
Maserati Bora          8
Volvo 142E             2

Caution! 특정 변수를 제일 앞으로 이동하는 작업은 함수 select(data, 특정 변수, everything())을 이용해서 할 수도 있다.


# 변수 wt를 변수 cyl 뒤로 이동 이동
data %>%
  relocate(wt, .after = cyl)
                     mpg cyl    wt  disp  hp drat  qsec vs am gear
Mazda RX4           21.0   6 2.620 160.0 110 3.90 16.46  0  1    4
Mazda RX4 Wag       21.0   6 2.875 160.0 110 3.90 17.02  0  1    4
Datsun 710          22.8   4 2.320 108.0  93 3.85 18.61  1  1    4
Hornet 4 Drive      21.4   6 3.215 258.0 110 3.08 19.44  1  0    3
Hornet Sportabout   18.7   8 3.440 360.0 175 3.15 17.02  0  0    3
Valiant             18.1   6 3.460 225.0 105 2.76 20.22  1  0    3
Duster 360          14.3   8 3.570 360.0 245 3.21 15.84  0  0    3
Merc 240D           24.4   4 3.190 146.7  62 3.69 20.00  1  0    4
Merc 230            22.8   4 3.150 140.8  95 3.92 22.90  1  0    4
Merc 280            19.2   6 3.440 167.6 123 3.92 18.30  1  0    4
Merc 280C           17.8   6 3.440 167.6 123 3.92 18.90  1  0    4
Merc 450SE          16.4   8 4.070 275.8 180 3.07 17.40  0  0    3
Merc 450SL          17.3   8 3.730 275.8 180 3.07 17.60  0  0    3
Merc 450SLC         15.2   8 3.780 275.8 180 3.07 18.00  0  0    3
Cadillac Fleetwood  10.4   8 5.250 472.0 205 2.93 17.98  0  0    3
Lincoln Continental 10.4   8 5.424 460.0 215 3.00 17.82  0  0    3
Chrysler Imperial   14.7   8 5.345 440.0 230 3.23 17.42  0  0    3
Fiat 128            32.4   4 2.200  78.7  66 4.08 19.47  1  1    4
Honda Civic         30.4   4 1.615  75.7  52 4.93 18.52  1  1    4
Toyota Corolla      33.9   4 1.835  71.1  65 4.22 19.90  1  1    4
Toyota Corona       21.5   4 2.465 120.1  97 3.70 20.01  1  0    3
Dodge Challenger    15.5   8 3.520 318.0 150 2.76 16.87  0  0    3
AMC Javelin         15.2   8 3.435 304.0 150 3.15 17.30  0  0    3
Camaro Z28          13.3   8 3.840 350.0 245 3.73 15.41  0  0    3
Pontiac Firebird    19.2   8 3.845 400.0 175 3.08 17.05  0  0    3
Fiat X1-9           27.3   4 1.935  79.0  66 4.08 18.90  1  1    4
Porsche 914-2       26.0   4 2.140 120.3  91 4.43 16.70  0  1    5
Lotus Europa        30.4   4 1.513  95.1 113 3.77 16.90  1  1    5
Ford Pantera L      15.8   8 3.170 351.0 264 4.22 14.50  0  1    5
Ferrari Dino        19.7   6 2.770 145.0 175 3.62 15.50  0  1    5
Maserati Bora       15.0   8 3.570 301.0 335 3.54 14.60  0  1    5
Volvo 142E          21.4   4 2.780 121.0 109 4.11 18.60  1  1    4
                    carb
Mazda RX4              4
Mazda RX4 Wag          4
Datsun 710             1
Hornet 4 Drive         1
Hornet Sportabout      2
Valiant                1
Duster 360             4
Merc 240D              2
Merc 230               2
Merc 280               4
Merc 280C              4
Merc 450SE             3
Merc 450SL             3
Merc 450SLC            3
Cadillac Fleetwood     4
Lincoln Continental    4
Chrysler Imperial      4
Fiat 128               1
Honda Civic            2
Toyota Corolla         1
Toyota Corona          1
Dodge Challenger       2
AMC Javelin            2
Camaro Z28             4
Pontiac Firebird       2
Fiat X1-9              1
Porsche 914-2          2
Lotus Europa           2
Ford Pantera L         4
Ferrari Dino           6
Maserati Bora          8
Volvo 142E             2

# 변수 wt를 변수 cyl 앞로 이동 이동
data %>%
  relocate(wt, .before = cyl)
                     mpg    wt cyl  disp  hp drat  qsec vs am gear
Mazda RX4           21.0 2.620   6 160.0 110 3.90 16.46  0  1    4
Mazda RX4 Wag       21.0 2.875   6 160.0 110 3.90 17.02  0  1    4
Datsun 710          22.8 2.320   4 108.0  93 3.85 18.61  1  1    4
Hornet 4 Drive      21.4 3.215   6 258.0 110 3.08 19.44  1  0    3
Hornet Sportabout   18.7 3.440   8 360.0 175 3.15 17.02  0  0    3
Valiant             18.1 3.460   6 225.0 105 2.76 20.22  1  0    3
Duster 360          14.3 3.570   8 360.0 245 3.21 15.84  0  0    3
Merc 240D           24.4 3.190   4 146.7  62 3.69 20.00  1  0    4
Merc 230            22.8 3.150   4 140.8  95 3.92 22.90  1  0    4
Merc 280            19.2 3.440   6 167.6 123 3.92 18.30  1  0    4
Merc 280C           17.8 3.440   6 167.6 123 3.92 18.90  1  0    4
Merc 450SE          16.4 4.070   8 275.8 180 3.07 17.40  0  0    3
Merc 450SL          17.3 3.730   8 275.8 180 3.07 17.60  0  0    3
Merc 450SLC         15.2 3.780   8 275.8 180 3.07 18.00  0  0    3
Cadillac Fleetwood  10.4 5.250   8 472.0 205 2.93 17.98  0  0    3
Lincoln Continental 10.4 5.424   8 460.0 215 3.00 17.82  0  0    3
Chrysler Imperial   14.7 5.345   8 440.0 230 3.23 17.42  0  0    3
Fiat 128            32.4 2.200   4  78.7  66 4.08 19.47  1  1    4
Honda Civic         30.4 1.615   4  75.7  52 4.93 18.52  1  1    4
Toyota Corolla      33.9 1.835   4  71.1  65 4.22 19.90  1  1    4
Toyota Corona       21.5 2.465   4 120.1  97 3.70 20.01  1  0    3
Dodge Challenger    15.5 3.520   8 318.0 150 2.76 16.87  0  0    3
AMC Javelin         15.2 3.435   8 304.0 150 3.15 17.30  0  0    3
Camaro Z28          13.3 3.840   8 350.0 245 3.73 15.41  0  0    3
Pontiac Firebird    19.2 3.845   8 400.0 175 3.08 17.05  0  0    3
Fiat X1-9           27.3 1.935   4  79.0  66 4.08 18.90  1  1    4
Porsche 914-2       26.0 2.140   4 120.3  91 4.43 16.70  0  1    5
Lotus Europa        30.4 1.513   4  95.1 113 3.77 16.90  1  1    5
Ford Pantera L      15.8 3.170   8 351.0 264 4.22 14.50  0  1    5
Ferrari Dino        19.7 2.770   6 145.0 175 3.62 15.50  0  1    5
Maserati Bora       15.0 3.570   8 301.0 335 3.54 14.60  0  1    5
Volvo 142E          21.4 2.780   4 121.0 109 4.11 18.60  1  1    4
                    carb
Mazda RX4              4
Mazda RX4 Wag          4
Datsun 710             1
Hornet 4 Drive         1
Hornet Sportabout      2
Valiant                1
Duster 360             4
Merc 240D              2
Merc 230               2
Merc 280               4
Merc 280C              4
Merc 450SE             3
Merc 450SL             3
Merc 450SLC            3
Cadillac Fleetwood     4
Lincoln Continental    4
Chrysler Imperial      4
Fiat 128               1
Honda Civic            2
Toyota Corolla         1
Toyota Corona          1
Dodge Challenger       2
AMC Javelin            2
Camaro Z28             4
Pontiac Firebird       2
Fiat X1-9              1
Porsche 914-2          2
Lotus Europa           2
Ford Pantera L         4
Ferrari Dino           6
Maserati Bora          8
Volvo 142E             2

# 변수 이름에 a가 포함되어 있는 변수들을 변수 cyl 앞으로 이동이동
data %>%
  relocate(contains("a"), .before = cyl)
                     mpg drat am gear carb cyl  disp  hp    wt  qsec
Mazda RX4           21.0 3.90  1    4    4   6 160.0 110 2.620 16.46
Mazda RX4 Wag       21.0 3.90  1    4    4   6 160.0 110 2.875 17.02
Datsun 710          22.8 3.85  1    4    1   4 108.0  93 2.320 18.61
Hornet 4 Drive      21.4 3.08  0    3    1   6 258.0 110 3.215 19.44
Hornet Sportabout   18.7 3.15  0    3    2   8 360.0 175 3.440 17.02
Valiant             18.1 2.76  0    3    1   6 225.0 105 3.460 20.22
Duster 360          14.3 3.21  0    3    4   8 360.0 245 3.570 15.84
Merc 240D           24.4 3.69  0    4    2   4 146.7  62 3.190 20.00
Merc 230            22.8 3.92  0    4    2   4 140.8  95 3.150 22.90
Merc 280            19.2 3.92  0    4    4   6 167.6 123 3.440 18.30
Merc 280C           17.8 3.92  0    4    4   6 167.6 123 3.440 18.90
Merc 450SE          16.4 3.07  0    3    3   8 275.8 180 4.070 17.40
Merc 450SL          17.3 3.07  0    3    3   8 275.8 180 3.730 17.60
Merc 450SLC         15.2 3.07  0    3    3   8 275.8 180 3.780 18.00
Cadillac Fleetwood  10.4 2.93  0    3    4   8 472.0 205 5.250 17.98
Lincoln Continental 10.4 3.00  0    3    4   8 460.0 215 5.424 17.82
Chrysler Imperial   14.7 3.23  0    3    4   8 440.0 230 5.345 17.42
Fiat 128            32.4 4.08  1    4    1   4  78.7  66 2.200 19.47
Honda Civic         30.4 4.93  1    4    2   4  75.7  52 1.615 18.52
Toyota Corolla      33.9 4.22  1    4    1   4  71.1  65 1.835 19.90
Toyota Corona       21.5 3.70  0    3    1   4 120.1  97 2.465 20.01
Dodge Challenger    15.5 2.76  0    3    2   8 318.0 150 3.520 16.87
AMC Javelin         15.2 3.15  0    3    2   8 304.0 150 3.435 17.30
Camaro Z28          13.3 3.73  0    3    4   8 350.0 245 3.840 15.41
Pontiac Firebird    19.2 3.08  0    3    2   8 400.0 175 3.845 17.05
Fiat X1-9           27.3 4.08  1    4    1   4  79.0  66 1.935 18.90
Porsche 914-2       26.0 4.43  1    5    2   4 120.3  91 2.140 16.70
Lotus Europa        30.4 3.77  1    5    2   4  95.1 113 1.513 16.90
Ford Pantera L      15.8 4.22  1    5    4   8 351.0 264 3.170 14.50
Ferrari Dino        19.7 3.62  1    5    6   6 145.0 175 2.770 15.50
Maserati Bora       15.0 3.54  1    5    8   8 301.0 335 3.570 14.60
Volvo 142E          21.4 4.11  1    4    2   4 121.0 109 2.780 18.60
                    vs
Mazda RX4            0
Mazda RX4 Wag        0
Datsun 710           1
Hornet 4 Drive       1
Hornet Sportabout    0
Valiant              1
Duster 360           0
Merc 240D            1
Merc 230             1
Merc 280             1
Merc 280C            1
Merc 450SE           0
Merc 450SL           0
Merc 450SLC          0
Cadillac Fleetwood   0
Lincoln Continental  0
Chrysler Imperial    0
Fiat 128             1
Honda Civic          1
Toyota Corolla       1
Toyota Corona        1
Dodge Challenger     0
AMC Javelin          0
Camaro Z28           0
Pontiac Firebird     0
Fiat X1-9            1
Porsche 914-2        0
Lotus Europa         1
Ford Pantera L       0
Ferrari Dino         0
Maserati Bora        0
Volvo 142E           1

5-5. 함수 mutate()와 transmute()

# 기존의 변수 mpg 값에 0.43을 곱한 변수 kml 생성 생성
# kml이 10 이상이면 "good", 10 미만이면 "bad" 값을 갖는 변수 gp_kml 생성data %>%
  mutate(kml    = 0.43*mpg,
         gp_kml = ifelse(kml >= 10, "good", "bad")) 
                     mpg cyl  disp  hp drat    wt  qsec vs am gear
Mazda RX4           21.0   6 160.0 110 3.90 2.620 16.46  0  1    4
Mazda RX4 Wag       21.0   6 160.0 110 3.90 2.875 17.02  0  1    4
Datsun 710          22.8   4 108.0  93 3.85 2.320 18.61  1  1    4
Hornet 4 Drive      21.4   6 258.0 110 3.08 3.215 19.44  1  0    3
Hornet Sportabout   18.7   8 360.0 175 3.15 3.440 17.02  0  0    3
Valiant             18.1   6 225.0 105 2.76 3.460 20.22  1  0    3
Duster 360          14.3   8 360.0 245 3.21 3.570 15.84  0  0    3
Merc 240D           24.4   4 146.7  62 3.69 3.190 20.00  1  0    4
Merc 230            22.8   4 140.8  95 3.92 3.150 22.90  1  0    4
Merc 280            19.2   6 167.6 123 3.92 3.440 18.30  1  0    4
Merc 280C           17.8   6 167.6 123 3.92 3.440 18.90  1  0    4
Merc 450SE          16.4   8 275.8 180 3.07 4.070 17.40  0  0    3
Merc 450SL          17.3   8 275.8 180 3.07 3.730 17.60  0  0    3
Merc 450SLC         15.2   8 275.8 180 3.07 3.780 18.00  0  0    3
Cadillac Fleetwood  10.4   8 472.0 205 2.93 5.250 17.98  0  0    3
Lincoln Continental 10.4   8 460.0 215 3.00 5.424 17.82  0  0    3
Chrysler Imperial   14.7   8 440.0 230 3.23 5.345 17.42  0  0    3
Fiat 128            32.4   4  78.7  66 4.08 2.200 19.47  1  1    4
Honda Civic         30.4   4  75.7  52 4.93 1.615 18.52  1  1    4
Toyota Corolla      33.9   4  71.1  65 4.22 1.835 19.90  1  1    4
Toyota Corona       21.5   4 120.1  97 3.70 2.465 20.01  1  0    3
Dodge Challenger    15.5   8 318.0 150 2.76 3.520 16.87  0  0    3
AMC Javelin         15.2   8 304.0 150 3.15 3.435 17.30  0  0    3
Camaro Z28          13.3   8 350.0 245 3.73 3.840 15.41  0  0    3
Pontiac Firebird    19.2   8 400.0 175 3.08 3.845 17.05  0  0    3
Fiat X1-9           27.3   4  79.0  66 4.08 1.935 18.90  1  1    4
Porsche 914-2       26.0   4 120.3  91 4.43 2.140 16.70  0  1    5
Lotus Europa        30.4   4  95.1 113 3.77 1.513 16.90  1  1    5
Ford Pantera L      15.8   8 351.0 264 4.22 3.170 14.50  0  1    5
Ferrari Dino        19.7   6 145.0 175 3.62 2.770 15.50  0  1    5
Maserati Bora       15.0   8 301.0 335 3.54 3.570 14.60  0  1    5
Volvo 142E          21.4   4 121.0 109 4.11 2.780 18.60  1  1    4
                    carb    kml gp_kml
Mazda RX4              4  9.030    bad
Mazda RX4 Wag          4  9.030    bad
Datsun 710             1  9.804    bad
Hornet 4 Drive         1  9.202    bad
Hornet Sportabout      2  8.041    bad
Valiant                1  7.783    bad
Duster 360             4  6.149    bad
Merc 240D              2 10.492   good
Merc 230               2  9.804    bad
Merc 280               4  8.256    bad
Merc 280C              4  7.654    bad
Merc 450SE             3  7.052    bad
Merc 450SL             3  7.439    bad
Merc 450SLC            3  6.536    bad
Cadillac Fleetwood     4  4.472    bad
Lincoln Continental    4  4.472    bad
Chrysler Imperial      4  6.321    bad
Fiat 128               1 13.932   good
Honda Civic            2 13.072   good
Toyota Corolla         1 14.577   good
Toyota Corona          1  9.245    bad
Dodge Challenger       2  6.665    bad
AMC Javelin            2  6.536    bad
Camaro Z28             4  5.719    bad
Pontiac Firebird       2  8.256    bad
Fiat X1-9              1 11.739   good
Porsche 914-2          2 11.180   good
Lotus Europa           2 13.072   good
Ford Pantera L         4  6.794    bad
Ferrari Dino           6  8.471    bad
Maserati Bora          8  6.450    bad
Volvo 142E             2  9.202    bad

Caution! 함수 ifelse(조건, true, false)는 조건을 만족하면 true의 값을 갖고, 아니면 false의 값을 갖는다. 이때 조건의 만족 여부에 따라 할당되는 두 가지 값의 유형은 같아야 한다.


# 기존의 변수 mpg 값에 0.43을 곱한 변수 kml 생성 생성
# kml이 11이상이면 "excellent", 11 미만 8 이상이면 "good", 8 미만이면 "bad" 값을 갖는 변수 gp_kml 생성
data %>%
  mutate(kml    = 0.43*mpg,
         gp_kml = case_when(kml >= 11            ~ "excellent",
                            8 <= kml & kml < 11 ~ "good", 
                            TRUE                 ~ "bad")) %>%
  relocate(kml, gp_kml)                                          # 맨 앞으로 이동
                       kml    gp_kml  mpg cyl  disp  hp drat    wt
Mazda RX4            9.030      good 21.0   6 160.0 110 3.90 2.620
Mazda RX4 Wag        9.030      good 21.0   6 160.0 110 3.90 2.875
Datsun 710           9.804      good 22.8   4 108.0  93 3.85 2.320
Hornet 4 Drive       9.202      good 21.4   6 258.0 110 3.08 3.215
Hornet Sportabout    8.041      good 18.7   8 360.0 175 3.15 3.440
Valiant              7.783       bad 18.1   6 225.0 105 2.76 3.460
Duster 360           6.149       bad 14.3   8 360.0 245 3.21 3.570
Merc 240D           10.492      good 24.4   4 146.7  62 3.69 3.190
Merc 230             9.804      good 22.8   4 140.8  95 3.92 3.150
Merc 280             8.256      good 19.2   6 167.6 123 3.92 3.440
Merc 280C            7.654       bad 17.8   6 167.6 123 3.92 3.440
Merc 450SE           7.052       bad 16.4   8 275.8 180 3.07 4.070
Merc 450SL           7.439       bad 17.3   8 275.8 180 3.07 3.730
Merc 450SLC          6.536       bad 15.2   8 275.8 180 3.07 3.780
Cadillac Fleetwood   4.472       bad 10.4   8 472.0 205 2.93 5.250
Lincoln Continental  4.472       bad 10.4   8 460.0 215 3.00 5.424
Chrysler Imperial    6.321       bad 14.7   8 440.0 230 3.23 5.345
Fiat 128            13.932 excellent 32.4   4  78.7  66 4.08 2.200
Honda Civic         13.072 excellent 30.4   4  75.7  52 4.93 1.615
Toyota Corolla      14.577 excellent 33.9   4  71.1  65 4.22 1.835
Toyota Corona        9.245      good 21.5   4 120.1  97 3.70 2.465
Dodge Challenger     6.665       bad 15.5   8 318.0 150 2.76 3.520
AMC Javelin          6.536       bad 15.2   8 304.0 150 3.15 3.435
Camaro Z28           5.719       bad 13.3   8 350.0 245 3.73 3.840
Pontiac Firebird     8.256      good 19.2   8 400.0 175 3.08 3.845
Fiat X1-9           11.739 excellent 27.3   4  79.0  66 4.08 1.935
Porsche 914-2       11.180 excellent 26.0   4 120.3  91 4.43 2.140
Lotus Europa        13.072 excellent 30.4   4  95.1 113 3.77 1.513
Ford Pantera L       6.794       bad 15.8   8 351.0 264 4.22 3.170
Ferrari Dino         8.471      good 19.7   6 145.0 175 3.62 2.770
Maserati Bora        6.450       bad 15.0   8 301.0 335 3.54 3.570
Volvo 142E           9.202      good 21.4   4 121.0 109 4.11 2.780
                     qsec vs am gear carb
Mazda RX4           16.46  0  1    4    4
Mazda RX4 Wag       17.02  0  1    4    4
Datsun 710          18.61  1  1    4    1
Hornet 4 Drive      19.44  1  0    3    1
Hornet Sportabout   17.02  0  0    3    2
Valiant             20.22  1  0    3    1
Duster 360          15.84  0  0    3    4
Merc 240D           20.00  1  0    4    2
Merc 230            22.90  1  0    4    2
Merc 280            18.30  1  0    4    4
Merc 280C           18.90  1  0    4    4
Merc 450SE          17.40  0  0    3    3
Merc 450SL          17.60  0  0    3    3
Merc 450SLC         18.00  0  0    3    3
Cadillac Fleetwood  17.98  0  0    3    4
Lincoln Continental 17.82  0  0    3    4
Chrysler Imperial   17.42  0  0    3    4
Fiat 128            19.47  1  1    4    1
Honda Civic         18.52  1  1    4    2
Toyota Corolla      19.90  1  1    4    1
Toyota Corona       20.01  1  0    3    1
Dodge Challenger    16.87  0  0    3    2
AMC Javelin         17.30  0  0    3    2
Camaro Z28          15.41  0  0    3    4
Pontiac Firebird    17.05  0  0    3    2
Fiat X1-9           18.90  1  1    4    1
Porsche 914-2       16.70  0  1    5    2
Lotus Europa        16.90  1  1    5    2
Ford Pantera L      14.50  0  1    5    4
Ferrari Dino        15.50  0  1    5    6
Maserati Bora       14.60  0  1    5    8
Volvo 142E          18.60  1  1    4    2

Caution! 함수 case_when(조건1 ~ value1, 조건2 ~ value2, TREU ~ value3)은 조건1을 만족하면 value1, 조건1은 만족하지 않으나 조건2를 만족하면 value2, 두 조건 모두 만족하지 않으면 value3의 값을 갖는다.


# 생성된 새로운 변수를 제외하고 기존의 변수 제거제거
data %>%
  transmute(kml    = 0.43*mpg,
            gp_kml = ifelse(kml >= 10, "good", "bad")) 
                       kml gp_kml
Mazda RX4            9.030    bad
Mazda RX4 Wag        9.030    bad
Datsun 710           9.804    bad
Hornet 4 Drive       9.202    bad
Hornet Sportabout    8.041    bad
Valiant              7.783    bad
Duster 360           6.149    bad
Merc 240D           10.492   good
Merc 230             9.804    bad
Merc 280             8.256    bad
Merc 280C            7.654    bad
Merc 450SE           7.052    bad
Merc 450SL           7.439    bad
Merc 450SLC          6.536    bad
Cadillac Fleetwood   4.472    bad
Lincoln Continental  4.472    bad
Chrysler Imperial    6.321    bad
Fiat 128            13.932   good
Honda Civic         13.072   good
Toyota Corolla      14.577   good
Toyota Corona        9.245    bad
Dodge Challenger     6.665    bad
AMC Javelin          6.536    bad
Camaro Z28           5.719    bad
Pontiac Firebird     8.256    bad
Fiat X1-9           11.739   good
Porsche 914-2       11.180   good
Lotus Europa        13.072   good
Ford Pantera L       6.794    bad
Ferrari Dino         8.471    bad
Maserati Bora        6.450    bad
Volvo 142E           9.202    bad

Caution! 함수 mutate()와 연관된 다른 함수로는 mutate_if(), mutate_at(), mutate_all()이 있다.

mutate_if() 조건을 만족하는 변수에 대해서만 함수 적용
mutate_at() 여러 변수에 동일한 함수를 적용하거나 하나의 변수에 여러 함수를 적용
mutate_all() 모든 변수에 함수 적용
# 변수에 입력된 값들의 총합이 100 미만인 열에 대해서만 지수 함수 적용NAdata %>%
  mutate_if(function(x) sum(x) < 100, exp)
                     mpg cyl  disp  hp drat    wt  qsec       vs
Mazda RX4           21.0   6 160.0 110 3.90 2.620 16.46 1.000000
Mazda RX4 Wag       21.0   6 160.0 110 3.90 2.875 17.02 1.000000
Datsun 710          22.8   4 108.0  93 3.85 2.320 18.61 2.718282
Hornet 4 Drive      21.4   6 258.0 110 3.08 3.215 19.44 2.718282
Hornet Sportabout   18.7   8 360.0 175 3.15 3.440 17.02 1.000000
Valiant             18.1   6 225.0 105 2.76 3.460 20.22 2.718282
Duster 360          14.3   8 360.0 245 3.21 3.570 15.84 1.000000
Merc 240D           24.4   4 146.7  62 3.69 3.190 20.00 2.718282
Merc 230            22.8   4 140.8  95 3.92 3.150 22.90 2.718282
Merc 280            19.2   6 167.6 123 3.92 3.440 18.30 2.718282
Merc 280C           17.8   6 167.6 123 3.92 3.440 18.90 2.718282
Merc 450SE          16.4   8 275.8 180 3.07 4.070 17.40 1.000000
Merc 450SL          17.3   8 275.8 180 3.07 3.730 17.60 1.000000
Merc 450SLC         15.2   8 275.8 180 3.07 3.780 18.00 1.000000
Cadillac Fleetwood  10.4   8 472.0 205 2.93 5.250 17.98 1.000000
Lincoln Continental 10.4   8 460.0 215 3.00 5.424 17.82 1.000000
Chrysler Imperial   14.7   8 440.0 230 3.23 5.345 17.42 1.000000
Fiat 128            32.4   4  78.7  66 4.08 2.200 19.47 2.718282
Honda Civic         30.4   4  75.7  52 4.93 1.615 18.52 2.718282
Toyota Corolla      33.9   4  71.1  65 4.22 1.835 19.90 2.718282
Toyota Corona       21.5   4 120.1  97 3.70 2.465 20.01 2.718282
Dodge Challenger    15.5   8 318.0 150 2.76 3.520 16.87 1.000000
AMC Javelin         15.2   8 304.0 150 3.15 3.435 17.30 1.000000
Camaro Z28          13.3   8 350.0 245 3.73 3.840 15.41 1.000000
Pontiac Firebird    19.2   8 400.0 175 3.08 3.845 17.05 1.000000
Fiat X1-9           27.3   4  79.0  66 4.08 1.935 18.90 2.718282
Porsche 914-2       26.0   4 120.3  91 4.43 2.140 16.70 1.000000
Lotus Europa        30.4   4  95.1 113 3.77 1.513 16.90 2.718282
Ford Pantera L      15.8   8 351.0 264 4.22 3.170 14.50 1.000000
Ferrari Dino        19.7   6 145.0 175 3.62 2.770 15.50 1.000000
Maserati Bora       15.0   8 301.0 335 3.54 3.570 14.60 1.000000
Volvo 142E          21.4   4 121.0 109 4.11 2.780 18.60 2.718282
                          am gear        carb
Mazda RX4           2.718282    4   54.598150
Mazda RX4 Wag       2.718282    4   54.598150
Datsun 710          2.718282    4    2.718282
Hornet 4 Drive      1.000000    3    2.718282
Hornet Sportabout   1.000000    3    7.389056
Valiant             1.000000    3    2.718282
Duster 360          1.000000    3   54.598150
Merc 240D           1.000000    4    7.389056
Merc 230            1.000000    4    7.389056
Merc 280            1.000000    4   54.598150
Merc 280C           1.000000    4   54.598150
Merc 450SE          1.000000    3   20.085537
Merc 450SL          1.000000    3   20.085537
Merc 450SLC         1.000000    3   20.085537
Cadillac Fleetwood  1.000000    3   54.598150
Lincoln Continental 1.000000    3   54.598150
Chrysler Imperial   1.000000    3   54.598150
Fiat 128            2.718282    4    2.718282
Honda Civic         2.718282    4    7.389056
Toyota Corolla      2.718282    4    2.718282
Toyota Corona       1.000000    3    2.718282
Dodge Challenger    1.000000    3    7.389056
AMC Javelin         1.000000    3    7.389056
Camaro Z28          1.000000    3   54.598150
Pontiac Firebird    1.000000    3    7.389056
Fiat X1-9           2.718282    4    2.718282
Porsche 914-2       2.718282    5    7.389056
Lotus Europa        2.718282    5    7.389056
Ford Pantera L      2.718282    5   54.598150
Ferrari Dino        2.718282    5  403.428793
Maserati Bora       2.718282    5 2980.957987
Volvo 142E          2.718282    4    7.389056

# 숫자형 변수들이면 범주형으로 변환NAa <- data %>%
  mutate_if(is.numeric, as.factor)
str(a)
'data.frame':   32 obs. of  11 variables:
 $ mpg : Factor w/ 25 levels "10.4","13.3",..: 16 16 19 17 13 12 3 20 19 14 ...
 $ cyl : Factor w/ 3 levels "4","6","8": 2 2 1 2 3 2 3 1 1 2 ...
 $ disp: Factor w/ 27 levels "71.1","75.7",..: 13 13 6 16 23 15 23 12 10 14 ...
 $ hp  : Factor w/ 22 levels "52","62","65",..: 11 11 6 11 15 9 20 2 7 13 ...
 $ drat: Factor w/ 22 levels "2.76","2.93",..: 16 16 15 5 6 1 7 11 17 17 ...
 $ wt  : Factor w/ 29 levels "1.513","1.615",..: 9 12 7 16 18 19 21 15 13 18 ...
 $ qsec: Factor w/ 30 levels "14.5","14.6",..: 6 10 22 24 10 29 5 27 30 19 ...
 $ vs  : Factor w/ 2 levels "0","1": 1 1 2 2 1 2 1 2 2 2 ...
 $ am  : Factor w/ 2 levels "0","1": 2 2 2 1 1 1 1 1 1 1 ...
 $ gear: Factor w/ 3 levels "3","4","5": 2 2 2 1 1 1 1 2 2 2 ...
 $ carb: Factor w/ 6 levels "1","2","3","4",..: 4 4 1 1 2 1 4 2 2 4 ...

# 변수 이름이 d로 시작하는 변수에 exp를 적용
data %>%
  mutate_at(vars(starts_with("d")), exp)                           
                     mpg cyl          disp  hp      drat    wt  qsec
Mazda RX4           21.0   6  3.069850e+69 110  49.40245 2.620 16.46
Mazda RX4 Wag       21.0   6  3.069850e+69 110  49.40245 2.875 17.02
Datsun 710          22.8   4  8.013164e+46  93  46.99306 2.320 18.61
Hornet 4 Drive      21.4   6 1.116802e+112 110  21.75840 3.215 19.44
Hornet Sportabout   18.7   8 2.218265e+156 175  23.33606 3.440 17.02
Valiant             18.1   6  5.203055e+97 105  15.79984 3.460 20.22
Duster 360          14.3   8 2.218265e+156 245  24.77909 3.570 15.84
Merc 240D           24.4   4  5.140442e+63  62  40.04485 3.190 20.00
Merc 230            22.8   4  1.408196e+61  95  50.40044 3.150 22.90
Merc 280            19.2   6  6.134161e+72 123  50.40044 3.440 18.30
Merc 280C           17.8   6  6.134161e+72 123  50.40044 3.440 18.90
Merc 450SE          16.4   8 6.003688e+119 180  21.54190 4.070 17.40
Merc 450SL          17.3   8 6.003688e+119 180  21.54190 3.730 17.60
Merc 450SLC         15.2   8 6.003688e+119 180  21.54190 3.780 18.00
Cadillac Fleetwood  10.4   8 9.704998e+204 205  18.72763 5.250 17.98
Lincoln Continental 10.4   8 5.962957e+199 215  20.08554 5.424 17.82
Chrysler Imperial   14.7   8 1.229057e+191 230  25.27966 5.345 17.42
Fiat 128            32.4   4  1.509996e+34  66  59.14547 2.200 19.47
Honda Civic         30.4   4  7.517826e+32  52 138.37951 1.615 18.52
Toyota Corolla      33.9   4  7.556795e+30  65  68.03348 1.835 19.90
Toyota Corona       21.5   4  1.441343e+52  97  40.44730 2.465 20.01
Dodge Challenger    15.5   8 1.275397e+138 150  15.79984 3.520 16.87
AMC Javelin         15.2   8 1.060529e+132 150  23.33606 3.435 17.30
Camaro Z28          13.3   8 1.007091e+152 245  41.67911 3.840 15.41
Pontiac Firebird    19.2   8 5.221470e+173 175  21.75840 3.845 17.05
Fiat X1-9           27.3   4  2.038281e+34  66  59.14547 1.935 18.90
Porsche 914-2       26.0   4  1.760460e+52  91  83.93142 2.140 16.70
Lotus Europa        30.4   4  2.001729e+41 113  43.38006 1.513 16.90
Ford Pantera L      15.8   8 2.737557e+152 264  68.03348 3.170 14.50
Ferrari Dino        19.7   6  9.390741e+62 175  37.33757 2.770 15.50
Maserati Bora       15.0   8 5.280062e+130 335  34.46692 3.570 14.60
Volvo 142E          21.4   4  3.545131e+52 109  60.94672 2.780 18.60
                    vs am gear carb
Mazda RX4            0  1    4    4
Mazda RX4 Wag        0  1    4    4
Datsun 710           1  1    4    1
Hornet 4 Drive       1  0    3    1
Hornet Sportabout    0  0    3    2
Valiant              1  0    3    1
Duster 360           0  0    3    4
Merc 240D            1  0    4    2
Merc 230             1  0    4    2
Merc 280             1  0    4    4
Merc 280C            1  0    4    4
Merc 450SE           0  0    3    3
Merc 450SL           0  0    3    3
Merc 450SLC          0  0    3    3
Cadillac Fleetwood   0  0    3    4
Lincoln Continental  0  0    3    4
Chrysler Imperial    0  0    3    4
Fiat 128             1  1    4    1
Honda Civic          1  1    4    2
Toyota Corolla       1  1    4    1
Toyota Corona        1  0    3    1
Dodge Challenger     0  0    3    2
AMC Javelin          0  0    3    2
Camaro Z28           0  0    3    4
Pontiac Firebird     0  0    3    2
Fiat X1-9            1  1    4    1
Porsche 914-2        0  1    5    2
Lotus Europa         1  1    5    2
Ford Pantera L       0  1    5    4
Ferrari Dino         0  1    5    6
Maserati Bora        0  1    5    8
Volvo 142E           1  1    4    2

Caution! mutate_at(data, vars(starts_with("d")), list(exp = exp))을 하면 지수 함수 exp를 적용한 새로운 변수가 추가된다.


# 변수 mpg와 disp에 로그 함수와 +2를 적용data %>%
  mutate_at(vars(mpg, disp), list(log = log, p2 = ~.x+2))
                     mpg cyl  disp  hp drat    wt  qsec vs am gear
Mazda RX4           21.0   6 160.0 110 3.90 2.620 16.46  0  1    4
Mazda RX4 Wag       21.0   6 160.0 110 3.90 2.875 17.02  0  1    4
Datsun 710          22.8   4 108.0  93 3.85 2.320 18.61  1  1    4
Hornet 4 Drive      21.4   6 258.0 110 3.08 3.215 19.44  1  0    3
Hornet Sportabout   18.7   8 360.0 175 3.15 3.440 17.02  0  0    3
Valiant             18.1   6 225.0 105 2.76 3.460 20.22  1  0    3
Duster 360          14.3   8 360.0 245 3.21 3.570 15.84  0  0    3
Merc 240D           24.4   4 146.7  62 3.69 3.190 20.00  1  0    4
Merc 230            22.8   4 140.8  95 3.92 3.150 22.90  1  0    4
Merc 280            19.2   6 167.6 123 3.92 3.440 18.30  1  0    4
Merc 280C           17.8   6 167.6 123 3.92 3.440 18.90  1  0    4
Merc 450SE          16.4   8 275.8 180 3.07 4.070 17.40  0  0    3
Merc 450SL          17.3   8 275.8 180 3.07 3.730 17.60  0  0    3
Merc 450SLC         15.2   8 275.8 180 3.07 3.780 18.00  0  0    3
Cadillac Fleetwood  10.4   8 472.0 205 2.93 5.250 17.98  0  0    3
Lincoln Continental 10.4   8 460.0 215 3.00 5.424 17.82  0  0    3
Chrysler Imperial   14.7   8 440.0 230 3.23 5.345 17.42  0  0    3
Fiat 128            32.4   4  78.7  66 4.08 2.200 19.47  1  1    4
Honda Civic         30.4   4  75.7  52 4.93 1.615 18.52  1  1    4
Toyota Corolla      33.9   4  71.1  65 4.22 1.835 19.90  1  1    4
Toyota Corona       21.5   4 120.1  97 3.70 2.465 20.01  1  0    3
Dodge Challenger    15.5   8 318.0 150 2.76 3.520 16.87  0  0    3
AMC Javelin         15.2   8 304.0 150 3.15 3.435 17.30  0  0    3
Camaro Z28          13.3   8 350.0 245 3.73 3.840 15.41  0  0    3
Pontiac Firebird    19.2   8 400.0 175 3.08 3.845 17.05  0  0    3
Fiat X1-9           27.3   4  79.0  66 4.08 1.935 18.90  1  1    4
Porsche 914-2       26.0   4 120.3  91 4.43 2.140 16.70  0  1    5
Lotus Europa        30.4   4  95.1 113 3.77 1.513 16.90  1  1    5
Ford Pantera L      15.8   8 351.0 264 4.22 3.170 14.50  0  1    5
Ferrari Dino        19.7   6 145.0 175 3.62 2.770 15.50  0  1    5
Maserati Bora       15.0   8 301.0 335 3.54 3.570 14.60  0  1    5
Volvo 142E          21.4   4 121.0 109 4.11 2.780 18.60  1  1    4
                    carb  mpg_log disp_log mpg_p2 disp_p2
Mazda RX4              4 3.044522 5.075174   23.0   162.0
Mazda RX4 Wag          4 3.044522 5.075174   23.0   162.0
Datsun 710             1 3.126761 4.682131   24.8   110.0
Hornet 4 Drive         1 3.063391 5.552960   23.4   260.0
Hornet Sportabout      2 2.928524 5.886104   20.7   362.0
Valiant                1 2.895912 5.416100   20.1   227.0
Duster 360             4 2.660260 5.886104   16.3   362.0
Merc 240D              2 3.194583 4.988390   26.4   148.7
Merc 230               2 3.126761 4.947340   24.8   142.8
Merc 280               4 2.954910 5.121580   21.2   169.6
Merc 280C              4 2.879198 5.121580   19.8   169.6
Merc 450SE             3 2.797281 5.619676   18.4   277.8
Merc 450SL             3 2.850707 5.619676   19.3   277.8
Merc 450SLC            3 2.721295 5.619676   17.2   277.8
Cadillac Fleetwood     4 2.341806 6.156979   12.4   474.0
Lincoln Continental    4 2.341806 6.131226   12.4   462.0
Chrysler Imperial      4 2.687847 6.086775   16.7   442.0
Fiat 128               1 3.478158 4.365643   34.4    80.7
Honda Civic            2 3.414443 4.326778   32.4    77.7
Toyota Corolla         1 3.523415 4.264087   35.9    73.1
Toyota Corona          1 3.068053 4.788325   23.5   122.1
Dodge Challenger       2 2.740840 5.762051   17.5   320.0
AMC Javelin            2 2.721295 5.717028   17.2   306.0
Camaro Z28             4 2.587764 5.857933   15.3   352.0
Pontiac Firebird       2 2.954910 5.991465   21.2   402.0
Fiat X1-9              1 3.306887 4.369448   29.3    81.0
Porsche 914-2          2 3.258097 4.789989   28.0   122.3
Lotus Europa           2 3.414443 4.554929   32.4    97.1
Ford Pantera L         4 2.760010 5.860786   17.8   353.0
Ferrari Dino           6 2.980619 4.976734   21.7   147.0
Maserati Bora          8 2.708050 5.707110   17.0   303.0
Volvo 142E             2 3.063391 4.795791   23.4   123.0

# 모든 변수에 지수 함수를 적용
data %>%
  mutate_all(exp)                                                 
                             mpg        cyl          disp
Mazda RX4           1.318816e+09  403.42879  3.069850e+69
Mazda RX4 Wag       1.318816e+09  403.42879  3.069850e+69
Datsun 710          7.978370e+09   54.59815  8.013164e+46
Hornet 4 Drive      1.967442e+09  403.42879 1.116802e+112
Hornet Sportabout   1.322229e+08 2980.95799 2.218265e+156
Valiant             7.256549e+07  403.42879  5.203055e+97
Duster 360          1.623346e+06 2980.95799 2.218265e+156
Merc 240D           3.951713e+10   54.59815  5.140442e+63
Merc 230            7.978370e+09   54.59815  1.408196e+61
Merc 280            2.179988e+08  403.42879  6.134161e+72
Merc 280C           5.375784e+07  403.42879  6.134161e+72
Merc 450SE          1.325652e+07 2980.95799 6.003688e+119
Merc 450SL          3.260578e+07 2980.95799 6.003688e+119
Merc 450SLC         3.992787e+06 2980.95799 6.003688e+119
Cadillac Fleetwood  3.285963e+04 2980.95799 9.704998e+204
Lincoln Continental 3.285963e+04 2980.95799 5.962957e+199
Chrysler Imperial   2.421748e+06 2980.95799 1.229057e+191
Fiat 128            1.177989e+14   54.59815  1.509996e+34
Honda Civic         1.594235e+13   54.59815  7.517826e+32
Toyota Corolla      5.279380e+14   54.59815  7.556795e+30
Toyota Corona       2.174360e+09   54.59815  1.441343e+52
Dodge Challenger    5.389698e+06 2980.95799 1.275397e+138
AMC Javelin         3.992787e+06 2980.95799 1.060529e+132
Camaro Z28          5.971956e+05 2980.95799 1.007091e+152
Pontiac Firebird    2.179988e+08 2980.95799 5.221470e+173
Fiat X1-9           7.181900e+11   54.59815  2.038281e+34
Porsche 914-2       1.957296e+11   54.59815  1.760460e+52
Lotus Europa        1.594235e+13   54.59815  2.001729e+41
Ford Pantera L      7.275332e+06 2980.95799 2.737557e+152
Ferrari Dino        3.594192e+08  403.42879  9.390741e+62
Maserati Bora       3.269017e+06 2980.95799 5.280062e+130
Volvo 142E          1.967442e+09   54.59815  3.545131e+52
                               hp      drat         wt       qsec
Mazda RX4            5.920972e+47  49.40245  13.735724   14076257
Mazda RX4 Wag        5.920972e+47  49.40245  17.725424   24642915
Datsun 710           2.451246e+40  46.99306  10.175674  120842669
Hornet 4 Drive       5.920972e+47  21.75840  24.903292  277130757
Hornet Sportabout    1.003539e+76  23.33606  31.186958   24642915
Valiant              3.989520e+45  15.79984  31.816977  604553060
Duster 360          2.524341e+106  24.77909  35.516593    7572244
Merc 240D            8.438357e+26  40.04485  24.288427  485165195
Merc 230             1.811239e+41  50.40044  23.336065 8817462790
Merc 280             2.619517e+53  50.40044  31.186958   88631688
Merc 280C            2.619517e+53  50.40044  31.186958  161497464
Merc 450SE           1.489384e+78  21.54190  58.556963   36034955
Merc 450SL           1.489384e+78  21.54190  41.679108   44013194
Merc 450SLC          1.489384e+78  21.54190  43.816042   65659969
Cadillac Fleetwood   1.072430e+89  18.72763 190.566268   64359815
Lincoln Continental  2.362183e+93  20.08554 226.784448   54843816
Chrysler Imperial    7.722018e+99  25.27966 209.557885   36762909
Fiat 128             4.607187e+28  59.14547   9.025013  285570645
Honda Civic          3.831008e+22 138.37951   5.027888  110441884
Toyota Corolla       1.694889e+28  68.03348   6.265134  438995623
Toyota Corona        1.338335e+42  40.44730  11.763482  490041187
Dodge Challenger     1.393710e+65  15.79984  33.784428   21210354
AMC Javelin          1.393710e+65  23.33606  31.031413   32605776
Camaro Z28          2.524341e+106  41.67911  46.525474    4925814
Pontiac Firebird     1.003539e+76  21.75840  46.758684   25393404
Fiat X1-9            4.607187e+28  59.14547   6.924044  161497464
Porsche 914-2        3.317400e+39  83.93142   8.499438   17894429
Lotus Europa         1.189259e+49  43.38006   4.540331   21856305
Ford Pantera L      4.505502e+114  68.03348  23.807484    1982759
Ferrari Dino         1.003539e+76  37.33757  15.958634    5389698
Maserati Bora       3.080714e+145  34.46692  35.516593    2191288
Volvo 142E           2.178204e+47  60.94672  16.119021  119640264
                          vs       am      gear        carb
Mazda RX4           1.000000 2.718282  54.59815   54.598150
Mazda RX4 Wag       1.000000 2.718282  54.59815   54.598150
Datsun 710          2.718282 2.718282  54.59815    2.718282
Hornet 4 Drive      2.718282 1.000000  20.08554    2.718282
Hornet Sportabout   1.000000 1.000000  20.08554    7.389056
Valiant             2.718282 1.000000  20.08554    2.718282
Duster 360          1.000000 1.000000  20.08554   54.598150
Merc 240D           2.718282 1.000000  54.59815    7.389056
Merc 230            2.718282 1.000000  54.59815    7.389056
Merc 280            2.718282 1.000000  54.59815   54.598150
Merc 280C           2.718282 1.000000  54.59815   54.598150
Merc 450SE          1.000000 1.000000  20.08554   20.085537
Merc 450SL          1.000000 1.000000  20.08554   20.085537
Merc 450SLC         1.000000 1.000000  20.08554   20.085537
Cadillac Fleetwood  1.000000 1.000000  20.08554   54.598150
Lincoln Continental 1.000000 1.000000  20.08554   54.598150
Chrysler Imperial   1.000000 1.000000  20.08554   54.598150
Fiat 128            2.718282 2.718282  54.59815    2.718282
Honda Civic         2.718282 2.718282  54.59815    7.389056
Toyota Corolla      2.718282 2.718282  54.59815    2.718282
Toyota Corona       2.718282 1.000000  20.08554    2.718282
Dodge Challenger    1.000000 1.000000  20.08554    7.389056
AMC Javelin         1.000000 1.000000  20.08554    7.389056
Camaro Z28          1.000000 1.000000  20.08554   54.598150
Pontiac Firebird    1.000000 1.000000  20.08554    7.389056
Fiat X1-9           2.718282 2.718282  54.59815    2.718282
Porsche 914-2       1.000000 2.718282 148.41316    7.389056
Lotus Europa        2.718282 2.718282 148.41316    7.389056
Ford Pantera L      1.000000 2.718282 148.41316   54.598150
Ferrari Dino        1.000000 2.718282 148.41316  403.428793
Maserati Bora       1.000000 2.718282 148.41316 2980.957987
Volvo 142E          2.718282 2.718282  54.59815    7.389056

Caution! mutate_all(data, list(exp = exp))을 하면 지수 함수 exp를 적용한 새로운 변수가 추가된다.


# 모든 변수에 지수 함수와 +2를 적용
data %>%
  mutate_all(list(exp = exp, p2 = ~.x+2))
                     mpg cyl  disp  hp drat    wt  qsec vs am gear
Mazda RX4           21.0   6 160.0 110 3.90 2.620 16.46  0  1    4
Mazda RX4 Wag       21.0   6 160.0 110 3.90 2.875 17.02  0  1    4
Datsun 710          22.8   4 108.0  93 3.85 2.320 18.61  1  1    4
Hornet 4 Drive      21.4   6 258.0 110 3.08 3.215 19.44  1  0    3
Hornet Sportabout   18.7   8 360.0 175 3.15 3.440 17.02  0  0    3
Valiant             18.1   6 225.0 105 2.76 3.460 20.22  1  0    3
Duster 360          14.3   8 360.0 245 3.21 3.570 15.84  0  0    3
Merc 240D           24.4   4 146.7  62 3.69 3.190 20.00  1  0    4
Merc 230            22.8   4 140.8  95 3.92 3.150 22.90  1  0    4
Merc 280            19.2   6 167.6 123 3.92 3.440 18.30  1  0    4
Merc 280C           17.8   6 167.6 123 3.92 3.440 18.90  1  0    4
Merc 450SE          16.4   8 275.8 180 3.07 4.070 17.40  0  0    3
Merc 450SL          17.3   8 275.8 180 3.07 3.730 17.60  0  0    3
Merc 450SLC         15.2   8 275.8 180 3.07 3.780 18.00  0  0    3
Cadillac Fleetwood  10.4   8 472.0 205 2.93 5.250 17.98  0  0    3
Lincoln Continental 10.4   8 460.0 215 3.00 5.424 17.82  0  0    3
Chrysler Imperial   14.7   8 440.0 230 3.23 5.345 17.42  0  0    3
Fiat 128            32.4   4  78.7  66 4.08 2.200 19.47  1  1    4
Honda Civic         30.4   4  75.7  52 4.93 1.615 18.52  1  1    4
Toyota Corolla      33.9   4  71.1  65 4.22 1.835 19.90  1  1    4
Toyota Corona       21.5   4 120.1  97 3.70 2.465 20.01  1  0    3
Dodge Challenger    15.5   8 318.0 150 2.76 3.520 16.87  0  0    3
AMC Javelin         15.2   8 304.0 150 3.15 3.435 17.30  0  0    3
Camaro Z28          13.3   8 350.0 245 3.73 3.840 15.41  0  0    3
Pontiac Firebird    19.2   8 400.0 175 3.08 3.845 17.05  0  0    3
Fiat X1-9           27.3   4  79.0  66 4.08 1.935 18.90  1  1    4
Porsche 914-2       26.0   4 120.3  91 4.43 2.140 16.70  0  1    5
Lotus Europa        30.4   4  95.1 113 3.77 1.513 16.90  1  1    5
Ford Pantera L      15.8   8 351.0 264 4.22 3.170 14.50  0  1    5
Ferrari Dino        19.7   6 145.0 175 3.62 2.770 15.50  0  1    5
Maserati Bora       15.0   8 301.0 335 3.54 3.570 14.60  0  1    5
Volvo 142E          21.4   4 121.0 109 4.11 2.780 18.60  1  1    4
                    carb      mpg_exp    cyl_exp      disp_exp
Mazda RX4              4 1.318816e+09  403.42879  3.069850e+69
Mazda RX4 Wag          4 1.318816e+09  403.42879  3.069850e+69
Datsun 710             1 7.978370e+09   54.59815  8.013164e+46
Hornet 4 Drive         1 1.967442e+09  403.42879 1.116802e+112
Hornet Sportabout      2 1.322229e+08 2980.95799 2.218265e+156
Valiant                1 7.256549e+07  403.42879  5.203055e+97
Duster 360             4 1.623346e+06 2980.95799 2.218265e+156
Merc 240D              2 3.951713e+10   54.59815  5.140442e+63
Merc 230               2 7.978370e+09   54.59815  1.408196e+61
Merc 280               4 2.179988e+08  403.42879  6.134161e+72
Merc 280C              4 5.375784e+07  403.42879  6.134161e+72
Merc 450SE             3 1.325652e+07 2980.95799 6.003688e+119
Merc 450SL             3 3.260578e+07 2980.95799 6.003688e+119
Merc 450SLC            3 3.992787e+06 2980.95799 6.003688e+119
Cadillac Fleetwood     4 3.285963e+04 2980.95799 9.704998e+204
Lincoln Continental    4 3.285963e+04 2980.95799 5.962957e+199
Chrysler Imperial      4 2.421748e+06 2980.95799 1.229057e+191
Fiat 128               1 1.177989e+14   54.59815  1.509996e+34
Honda Civic            2 1.594235e+13   54.59815  7.517826e+32
Toyota Corolla         1 5.279380e+14   54.59815  7.556795e+30
Toyota Corona          1 2.174360e+09   54.59815  1.441343e+52
Dodge Challenger       2 5.389698e+06 2980.95799 1.275397e+138
AMC Javelin            2 3.992787e+06 2980.95799 1.060529e+132
Camaro Z28             4 5.971956e+05 2980.95799 1.007091e+152
Pontiac Firebird       2 2.179988e+08 2980.95799 5.221470e+173
Fiat X1-9              1 7.181900e+11   54.59815  2.038281e+34
Porsche 914-2          2 1.957296e+11   54.59815  1.760460e+52
Lotus Europa           2 1.594235e+13   54.59815  2.001729e+41
Ford Pantera L         4 7.275332e+06 2980.95799 2.737557e+152
Ferrari Dino           6 3.594192e+08  403.42879  9.390741e+62
Maserati Bora          8 3.269017e+06 2980.95799 5.280062e+130
Volvo 142E             2 1.967442e+09   54.59815  3.545131e+52
                           hp_exp  drat_exp     wt_exp   qsec_exp
Mazda RX4            5.920972e+47  49.40245  13.735724   14076257
Mazda RX4 Wag        5.920972e+47  49.40245  17.725424   24642915
Datsun 710           2.451246e+40  46.99306  10.175674  120842669
Hornet 4 Drive       5.920972e+47  21.75840  24.903292  277130757
Hornet Sportabout    1.003539e+76  23.33606  31.186958   24642915
Valiant              3.989520e+45  15.79984  31.816977  604553060
Duster 360          2.524341e+106  24.77909  35.516593    7572244
Merc 240D            8.438357e+26  40.04485  24.288427  485165195
Merc 230             1.811239e+41  50.40044  23.336065 8817462790
Merc 280             2.619517e+53  50.40044  31.186958   88631688
Merc 280C            2.619517e+53  50.40044  31.186958  161497464
Merc 450SE           1.489384e+78  21.54190  58.556963   36034955
Merc 450SL           1.489384e+78  21.54190  41.679108   44013194
Merc 450SLC          1.489384e+78  21.54190  43.816042   65659969
Cadillac Fleetwood   1.072430e+89  18.72763 190.566268   64359815
Lincoln Continental  2.362183e+93  20.08554 226.784448   54843816
Chrysler Imperial    7.722018e+99  25.27966 209.557885   36762909
Fiat 128             4.607187e+28  59.14547   9.025013  285570645
Honda Civic          3.831008e+22 138.37951   5.027888  110441884
Toyota Corolla       1.694889e+28  68.03348   6.265134  438995623
Toyota Corona        1.338335e+42  40.44730  11.763482  490041187
Dodge Challenger     1.393710e+65  15.79984  33.784428   21210354
AMC Javelin          1.393710e+65  23.33606  31.031413   32605776
Camaro Z28          2.524341e+106  41.67911  46.525474    4925814
Pontiac Firebird     1.003539e+76  21.75840  46.758684   25393404
Fiat X1-9            4.607187e+28  59.14547   6.924044  161497464
Porsche 914-2        3.317400e+39  83.93142   8.499438   17894429
Lotus Europa         1.189259e+49  43.38006   4.540331   21856305
Ford Pantera L      4.505502e+114  68.03348  23.807484    1982759
Ferrari Dino         1.003539e+76  37.33757  15.958634    5389698
Maserati Bora       3.080714e+145  34.46692  35.516593    2191288
Volvo 142E           2.178204e+47  60.94672  16.119021  119640264
                      vs_exp   am_exp  gear_exp    carb_exp mpg_p2
Mazda RX4           1.000000 2.718282  54.59815   54.598150   23.0
Mazda RX4 Wag       1.000000 2.718282  54.59815   54.598150   23.0
Datsun 710          2.718282 2.718282  54.59815    2.718282   24.8
Hornet 4 Drive      2.718282 1.000000  20.08554    2.718282   23.4
Hornet Sportabout   1.000000 1.000000  20.08554    7.389056   20.7
Valiant             2.718282 1.000000  20.08554    2.718282   20.1
Duster 360          1.000000 1.000000  20.08554   54.598150   16.3
Merc 240D           2.718282 1.000000  54.59815    7.389056   26.4
Merc 230            2.718282 1.000000  54.59815    7.389056   24.8
Merc 280            2.718282 1.000000  54.59815   54.598150   21.2
Merc 280C           2.718282 1.000000  54.59815   54.598150   19.8
Merc 450SE          1.000000 1.000000  20.08554   20.085537   18.4
Merc 450SL          1.000000 1.000000  20.08554   20.085537   19.3
Merc 450SLC         1.000000 1.000000  20.08554   20.085537   17.2
Cadillac Fleetwood  1.000000 1.000000  20.08554   54.598150   12.4
Lincoln Continental 1.000000 1.000000  20.08554   54.598150   12.4
Chrysler Imperial   1.000000 1.000000  20.08554   54.598150   16.7
Fiat 128            2.718282 2.718282  54.59815    2.718282   34.4
Honda Civic         2.718282 2.718282  54.59815    7.389056   32.4
Toyota Corolla      2.718282 2.718282  54.59815    2.718282   35.9
Toyota Corona       2.718282 1.000000  20.08554    2.718282   23.5
Dodge Challenger    1.000000 1.000000  20.08554    7.389056   17.5
AMC Javelin         1.000000 1.000000  20.08554    7.389056   17.2
Camaro Z28          1.000000 1.000000  20.08554   54.598150   15.3
Pontiac Firebird    1.000000 1.000000  20.08554    7.389056   21.2
Fiat X1-9           2.718282 2.718282  54.59815    2.718282   29.3
Porsche 914-2       1.000000 2.718282 148.41316    7.389056   28.0
Lotus Europa        2.718282 2.718282 148.41316    7.389056   32.4
Ford Pantera L      1.000000 2.718282 148.41316   54.598150   17.8
Ferrari Dino        1.000000 2.718282 148.41316  403.428793   21.7
Maserati Bora       1.000000 2.718282 148.41316 2980.957987   17.0
Volvo 142E          2.718282 2.718282  54.59815    7.389056   23.4
                    cyl_p2 disp_p2 hp_p2 drat_p2 wt_p2 qsec_p2 vs_p2
Mazda RX4                8   162.0   112    5.90 4.620   18.46     2
Mazda RX4 Wag            8   162.0   112    5.90 4.875   19.02     2
Datsun 710               6   110.0    95    5.85 4.320   20.61     3
Hornet 4 Drive           8   260.0   112    5.08 5.215   21.44     3
Hornet Sportabout       10   362.0   177    5.15 5.440   19.02     2
Valiant                  8   227.0   107    4.76 5.460   22.22     3
Duster 360              10   362.0   247    5.21 5.570   17.84     2
Merc 240D                6   148.7    64    5.69 5.190   22.00     3
Merc 230                 6   142.8    97    5.92 5.150   24.90     3
Merc 280                 8   169.6   125    5.92 5.440   20.30     3
Merc 280C                8   169.6   125    5.92 5.440   20.90     3
Merc 450SE              10   277.8   182    5.07 6.070   19.40     2
Merc 450SL              10   277.8   182    5.07 5.730   19.60     2
Merc 450SLC             10   277.8   182    5.07 5.780   20.00     2
Cadillac Fleetwood      10   474.0   207    4.93 7.250   19.98     2
Lincoln Continental     10   462.0   217    5.00 7.424   19.82     2
Chrysler Imperial       10   442.0   232    5.23 7.345   19.42     2
Fiat 128                 6    80.7    68    6.08 4.200   21.47     3
Honda Civic              6    77.7    54    6.93 3.615   20.52     3
Toyota Corolla           6    73.1    67    6.22 3.835   21.90     3
Toyota Corona            6   122.1    99    5.70 4.465   22.01     3
Dodge Challenger        10   320.0   152    4.76 5.520   18.87     2
AMC Javelin             10   306.0   152    5.15 5.435   19.30     2
Camaro Z28              10   352.0   247    5.73 5.840   17.41     2
Pontiac Firebird        10   402.0   177    5.08 5.845   19.05     2
Fiat X1-9                6    81.0    68    6.08 3.935   20.90     3
Porsche 914-2            6   122.3    93    6.43 4.140   18.70     2
Lotus Europa             6    97.1   115    5.77 3.513   18.90     3
Ford Pantera L          10   353.0   266    6.22 5.170   16.50     2
Ferrari Dino             8   147.0   177    5.62 4.770   17.50     2
Maserati Bora           10   303.0   337    5.54 5.570   16.60     2
Volvo 142E               6   123.0   111    6.11 4.780   20.60     3
                    am_p2 gear_p2 carb_p2
Mazda RX4               3       6       6
Mazda RX4 Wag           3       6       6
Datsun 710              3       6       3
Hornet 4 Drive          2       5       3
Hornet Sportabout       2       5       4
Valiant                 2       5       3
Duster 360              2       5       6
Merc 240D               2       6       4
Merc 230                2       6       4
Merc 280                2       6       6
Merc 280C               2       6       6
Merc 450SE              2       5       5
Merc 450SL              2       5       5
Merc 450SLC             2       5       5
Cadillac Fleetwood      2       5       6
Lincoln Continental     2       5       6
Chrysler Imperial       2       5       6
Fiat 128                3       6       3
Honda Civic             3       6       4
Toyota Corolla          3       6       3
Toyota Corona           2       5       3
Dodge Challenger        2       5       4
AMC Javelin             2       5       4
Camaro Z28              2       5       6
Pontiac Firebird        2       5       4
Fiat X1-9               3       6       3
Porsche 914-2           3       7       4
Lotus Europa            3       7       4
Ford Pantera L          3       7       6
Ferrari Dino            3       7       8
Maserati Bora           3       7      10
Volvo 142E              3       6       4

5-6. 함수 bind_cols()

x <- data.frame(x1 = 1:4, x2 = c("가"","나"나""다"다""라"))))
y <- data.frame(x3 = 3:6, x4 = c("a", "b", "c", "d"))

bind_cols(x, y)
  x1 x2 x3 x4
1  1 가  3  a
2  2 나  4  b
3  3 다  5  c
4  4 라  6  d

5-7. 함수 across()

# 모든 숫자형 변수들의 평균 계산NAdata %>%
  summarise(across(where(is.numeric), mean))
       mpg    cyl     disp       hp     drat      wt     qsec     vs
1 20.09062 6.1875 230.7219 146.6875 3.596563 3.21725 17.84875 0.4375
       am   gear   carb
1 0.40625 3.6875 2.8125

# 모든 숫자형 변수들의 결측값을 제거한 평균 계산NAdata %>%
  summarise(across(where(is.numeric), ~mean(.x, na.rm = TRUE)))
       mpg    cyl     disp       hp     drat      wt     qsec     vs
1 20.09062 6.1875 230.7219 146.6875 3.596563 3.21725 17.84875 0.4375
       am   gear   carb
1 0.40625 3.6875 2.8125

Caution! 옵션 na.rm = TRUE를 하면 결측값을 제거한 후 평균을 계산한다.


# 변수 이름에 a가 포함되어 있는 변수들만 평균과 표준편차NAdata %>%
  summarise(across(contains("a"), list(Avg = mean, SD = sd)))
  drat_Avg   drat_SD  am_Avg     am_SD gear_Avg   gear_SD carb_Avg
1 3.596563 0.5346787 0.40625 0.4989909   3.6875 0.7378041   2.8125
  carb_SD
1  1.6152

# 변수 mpg와 cyl의 결측값 개수data %>%
  summarise(across(c(mpg, cyl), ~sum(is.na(.x))))
  mpg cyl
1   0   0

# 변수 cyl와 am를 범주형으로 변환a <- data %>%
  mutate(across(c(cyl, am), as.factor))

str(a)
'data.frame':   32 obs. of  11 variables:
 $ mpg : num  21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...
 $ cyl : Factor w/ 3 levels "4","6","8": 2 2 1 2 3 2 3 1 1 2 ...
 $ disp: num  160 160 108 258 360 ...
 $ hp  : num  110 110 93 110 175 105 245 62 95 123 ...
 $ drat: num  3.9 3.9 3.85 3.08 3.15 2.76 3.21 3.69 3.92 3.92 ...
 $ wt  : num  2.62 2.88 2.32 3.21 3.44 ...
 $ qsec: num  16.5 17 18.6 19.4 17 ...
 $ vs  : num  0 0 1 1 0 1 0 1 1 1 ...
 $ am  : Factor w/ 2 levels "0","1": 2 2 2 1 1 1 1 1 1 1 ...
 $ gear: num  4 4 4 3 3 3 3 4 4 4 ...
 $ carb: num  4 4 1 1 2 1 4 2 2 4 ...

6. 함수 summarise


data %>%
  summarise(n = n(), n_cyl = n_distinct(cyl), avg_mpg = mean(mpg))
   n n_cyl  avg_mpg
1 32     3 20.09062

Result! 총 데이터 포인트 개수는 32개이고, 변수 “cyl”는 총 3개의 값으로 되어 있으며, 변수 “mpg”의 평균은 20.09062이다.


data %>%
  summarise(max_mpg = max(mpg), range_hp = range(hp))
  max_mpg range_hp
1    33.9       52
2    33.9      335

Result! 변수 “mpg”의 최대값은 33.9이고, 변수 “hp”의 최소값과 최대값은 각각 52와 335이다.


Caution! 함수 summarise_at()을 이용하여 지정한 여러 변수들에 함수를 한꺼번에 적용하거나 하나의 변수에 여러 함수를 적용할 수 있다.

# 변수 mpg와 wt의 최소값, 최대값, 중앙값
data %>%
  summarise_at(vars(mpg, wt), list(min = min, max = max, median = median))     # summarise_at(vars(mpg, wt), funs(min, max, median))
  mpg_min wt_min mpg_max wt_max mpg_median wt_median
1    10.4  1.513    33.9  5.424       19.2     3.325

Caution! 함수 summarise_all()을 이용하여 모든 변수에 함수를 한꺼번에 적용할 수 있다.

# 모든 변수의 평균과 75분위수data %>%
  summarise_all(list(mean = mean, quantile = ~quantile(.x, prob = 0.75))) 
  mpg_mean cyl_mean disp_mean  hp_mean drat_mean wt_mean qsec_mean
1 20.09062   6.1875  230.7219 146.6875  3.596563 3.21725  17.84875
  vs_mean am_mean gear_mean carb_mean mpg_quantile cyl_quantile
1  0.4375 0.40625    3.6875    2.8125         22.8            8
  disp_quantile hp_quantile drat_quantile wt_quantile qsec_quantile
1           326         180          3.92        3.61          18.9
  vs_quantile am_quantile gear_quantile carb_quantile
1           1           1             4             4

7. 함수 group_by

data %>%
  group_by(cyl)
# A tibble: 32 x 11
# Groups:   cyl [3]
     mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb
   <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
 1  21       6  160    110  3.9   2.62  16.5     0     1     4     4
 2  21       6  160    110  3.9   2.88  17.0     0     1     4     4
 3  22.8     4  108     93  3.85  2.32  18.6     1     1     4     1
 4  21.4     6  258    110  3.08  3.22  19.4     1     0     3     1
 5  18.7     8  360    175  3.15  3.44  17.0     0     0     3     2
 6  18.1     6  225    105  2.76  3.46  20.2     1     0     3     1
 7  14.3     8  360    245  3.21  3.57  15.8     0     0     3     4
 8  24.4     4  147.    62  3.69  3.19  20       1     0     4     2
 9  22.8     4  141.    95  3.92  3.15  22.9     1     0     4     2
10  19.2     6  168.   123  3.92  3.44  18.3     1     0     4     4
# ... with 22 more rows

Result! 변수 “cyl”에 의해 3개 그룹이 구성되었다.


data %>%
  group_by(cyl, am)
# A tibble: 32 x 11
# Groups:   cyl, am [6]
     mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb
   <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
 1  21       6  160    110  3.9   2.62  16.5     0     1     4     4
 2  21       6  160    110  3.9   2.88  17.0     0     1     4     4
 3  22.8     4  108     93  3.85  2.32  18.6     1     1     4     1
 4  21.4     6  258    110  3.08  3.22  19.4     1     0     3     1
 5  18.7     8  360    175  3.15  3.44  17.0     0     0     3     2
 6  18.1     6  225    105  2.76  3.46  20.2     1     0     3     1
 7  14.3     8  360    245  3.21  3.57  15.8     0     0     3     4
 8  24.4     4  147.    62  3.69  3.19  20       1     0     4     2
 9  22.8     4  141.    95  3.92  3.15  22.9     1     0     4     2
10  19.2     6  168.   123  3.92  3.44  18.3     1     0     4     4
# ... with 22 more rows

Result! 변수 “cyl”와 “am”에 의해 6개 그룹이 구성되었다.


Caution! 각 그룹에 속한 데이터 포인트의 개수는 함수 tally()를 사용하면 알 수 있다.

data %>%
  group_by(cyl, am) %>%
  tally()
# A tibble: 6 x 3
# Groups:   cyl [3]
    cyl    am     n
  <dbl> <dbl> <int>
1     4     0     3
2     4     1     8
3     6     0     4
4     6     1     3
5     8     0    12
6     8     1     2

7-1. 그룹별 요약

# cyl에 따른 변수 mpg의 평균data %>%
  group_by(cyl) %>%
  summarise(avg_mpg = mean(mpg))
# A tibble: 3 x 2
    cyl avg_mpg
  <dbl>   <dbl>
1     4    26.7
2     6    19.7
3     8    15.1

# cyl에 따른 변수 disp의 합과 변수 hp 범위범위
data %>%
  group_by(cyl) %>%
  summarise(sum_disp = sum(disp), range_hp = range(hp))
# A tibble: 6 x 3
# Groups:   cyl [3]
    cyl sum_disp range_hp
  <dbl>    <dbl>    <dbl>
1     4    1156.       52
2     4    1156.      113
3     6    1283.      105
4     6    1283.      175
5     8    4943.      150
6     8    4943.      335

# cyl와 am에 따른 변수 wt의 사분위수위수
data %>%
  group_by(cyl, am) %>%
  summarise(quantile_wt = quantile(wt, prob = c(0.25, 0.5, 0.75)))
# A tibble: 18 x 3
# Groups:   cyl, am [6]
     cyl    am quantile_wt
   <dbl> <dbl>       <dbl>
 1     4     0        2.81
 2     4     0        3.15
 3     4     0        3.17
 4     4     1        1.78
 5     4     1        2.04
 6     4     1        2.23
 7     6     0        3.38
 8     6     0        3.44
 9     6     0        3.44
10     6     1        2.70
11     6     1        2.77
12     6     1        2.82
13     8     0        3.56
14     8     0        3.81
15     8     0        4.36
16     8     1        3.27
17     8     1        3.37
18     8     1        3.47

7-2. 그룹별 정렬

data %>%
  group_by(cyl) %>%
  arrange(mpg, .by_group = TRUE)
# A tibble: 32 x 11
# Groups:   cyl [3]
     mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb
   <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
 1  21.4     4 121     109  4.11  2.78  18.6     1     1     4     2
 2  21.5     4 120.     97  3.7   2.46  20.0     1     0     3     1
 3  22.8     4 108      93  3.85  2.32  18.6     1     1     4     1
 4  22.8     4 141.     95  3.92  3.15  22.9     1     0     4     2
 5  24.4     4 147.     62  3.69  3.19  20       1     0     4     2
 6  26       4 120.     91  4.43  2.14  16.7     0     1     5     2
 7  27.3     4  79      66  4.08  1.94  18.9     1     1     4     1
 8  30.4     4  75.7    52  4.93  1.62  18.5     1     1     4     2
 9  30.4     4  95.1   113  3.77  1.51  16.9     1     1     5     2
10  32.4     4  78.7    66  4.08  2.2   19.5     1     1     4     1
# ... with 22 more rows

Result! 옵션 .by_group = TRUE를 하면 그룹 변수가 첫 번째 정렬 변수로 사용되기 때문에 그룹 변수 “cyl”로 먼저 정렬한 후 각 그룹 내에서 변수 “mpg”에 의해 오름차순으로 정렬하였다.

Reuse

Text and figures are licensed under Creative Commons Attribution CC BY 4.0. The figures that have been reused from other sources don't fall under this license and can be recognized by a note in their caption: "Figure from ...".