DS Study/R4DS(R언어)

[R4DS] [2-5] mutate()

23학번이수현 2024. 3. 31. 20:49

1.mutate()

- 새로운 열을 추가하는 함수 

- mutate()는 새로운 열을 항상 dataset 마지막에 추가함 

-ex)

r <- select(flights,year:day,distance,air_time)
# A tibble: 336,776 × 5
    year month   day distance air_time
   <int> <int> <int>    <dbl>    <dbl>
 1  2013     1     1     1400      227
 2  2013     1     1     1416      227
 3  2013     1     1     1089      160
 4  2013     1     1     1576      183
 5  2013     1     1      762      116
 6  2013     1     1      719      150
 7  2013     1     1     1065      158
 8  2013     1     1      229       53
 9  2013     1     1      944      140
10  2013     1     1      733      138
# ℹ 336,766 more rows
# ℹ Use `print(n = ...)` to see more rows
mutate(r,speed=distance/air_time*60)
# A tibble: 336,776 × 6
    year month   day distance air_time speed
   <int> <int> <int>    <dbl>    <dbl> <dbl>
 1  2013     1     1     1400      227  370.
 2  2013     1     1     1416      227  374.
 3  2013     1     1     1089      160  408.
 4  2013     1     1     1576      183  517.
 5  2013     1     1      762      116  394.
 6  2013     1     1      719      150  288.
 7  2013     1     1     1065      158  404.
 8  2013     1     1      229       53  259.
 9  2013     1     1      944      140  405.
10  2013     1     1      733      138  319.
# ℹ 336,766 more rows
# ℹ Use `print(n = ...)` to see more rows

2.transmute()

- 새로 추가한 열만 표시하고 싶을 때 사용하는 함수

ex) 

transmute(r,speed=distance/air_time*60)
# A tibble: 336,776 × 1
   speed
   <dbl>
 1  370.
 2  374.
 3  408.
 4  517.
 5  394.
 6  288.
 7  404.
 8  259.
 9  405.
10  319.
# ℹ 336,766 more rows
# ℹ Use `print(n = ...)` to see more rows

3. 유용한 생성함수 

1) 산술연산자 : +,-,*,/,^

 

2)모듈러연산

- %/% : 몫

- %% : 나머지 

3) 누적 

- cumsum() -> 누적합

- cummean() -> 누적평균