dplyr ํจํค์ง ํ์ด๋ณด๊ธฐ
|
|
๋ชฉ์ฐจ
- rowwise
1-1. pmax - slice
- relocate
- lag, lead
- between, near
- coalsece
- recode
- first, last, nth
- rownames_to_column, column_to_rownames
- bind_rows, bind_cols
- mutate_all, mutate_if
- inner_join, left_join, right_join, full_join
- semi_join, anti_join
๋ฐ์ดํฐ
|
|
## # A tibble: 5 x 3
## x y z
## <dbl> <dbl> <dbl>
## 1 6 7 -1
## 2 5 4 -2
## 3 5 2 6
## 4 6 2 12
## 5 5 4 2
|
|
## Rows: 237
## Columns: 12
## $ Sex <fct> Female, Male, Male, Male, Male, Female, Male, Female, Male, ...
## $ Wr.Hnd <dbl> 18.5, 19.5, 18.0, 18.8, 20.0, 18.0, 17.7, 17.0, 20.0, 18.5, ...
## $ NW.Hnd <dbl> 18.0, 20.5, 13.3, 18.9, 20.0, 17.7, 17.7, 17.3, 19.5, 18.5, ...
## $ W.Hnd <fct> Right, Left, Right, Right, Right, Right, Right, Right, Right...
## $ Fold <fct> R on L, R on L, L on R, R on L, Neither, L on R, L on R, R o...
## $ Pulse <int> 92, 104, 87, NA, 35, 64, 83, 74, 72, 90, 80, 68, NA, 66, 60,...
## $ Clap <fct> Left, Left, Neither, Neither, Right, Right, Right, Right, Ri...
## $ Exer <fct> Some, None, None, None, Some, Some, Freq, Freq, Some, Some, ...
## $ Smoke <fct> Never, Regul, Occas, Never, Never, Never, Never, Never, Neve...
## $ Height <dbl> 173.00, 177.80, NA, 160.00, 165.00, 172.72, 182.88, 157.00, ...
## $ M.I <fct> Metric, Imperial, NA, Metric, Metric, Imperial, Imperial, Me...
## $ Age <dbl> 18.250, 17.583, 16.917, 20.333, 23.667, 21.000, 18.833, 35.8...
1. rowwise()
ํ๋ณ๋ก ์ต๋๊ฐ ๊ตฌํ๊ธฐ
|
|
## # A tibble: 5 x 4
## # Rowwise:
## x y z max
## <dbl> <dbl> <dbl> <dbl>
## 1 6 7 -1 7
## 2 5 4 -2 5
## 3 5 2 6 6
## 4 6 2 12 12
## 5 5 4 2 5
|
|
## # A tibble: 5 x 4
## x y z max
## <dbl> <dbl> <dbl> <dbl>
## 1 6 7 -1 12
## 2 5 4 -2 12
## 3 5 2 6 12
## 4 6 2 12 12
## 5 5 4 2 12
1-1. pmax
๊ทธ๋ฐ๋ฐ ์ฌ์ค์ ์ฌ๊ธฐ์ rowwise๋ฅผ ์ฌ์ฉํ์ง ์๊ณ , pmax๋ฅผ ์ฌ์ฉํ๋ฉด ๋ณด๋ค ๊ฐ๋จํ๊ฒ ๊ตฌํ ์ ์๊ธฐ๋ ํ๋ค.
|
|
## # A tibble: 5 x 4
## x y z max
## <dbl> <dbl> <dbl> <dbl>
## 1 6 7 -1 7
## 2 5 4 -2 5
## 3 5 2 6 6
## 4 6 2 12 12
## 5 5 4 2 5
2. slice()
ํ ์ ํ
|
|
## Sex Wr.Hnd NW.Hnd W.Hnd Fold Pulse Clap Exer Smoke Height M.I
## 1 Male 20.0 20.0 Right Neither 35 Right Some Never 165.00 Metric
## 2 Female 16.5 17.0 Right L on R 40 Left Freq Never 167.64 Imperial
## 3 Female 18.0 17.5 Right R on L 48 Neither Freq Never 165.00 Metric
## 4 Male 21.0 21.0 Right L on R 48 Neither Freq Never 174.00 Metric
## 5 Female 18.0 17.9 Right R on L 50 Left None Never 165.00 Metric
## 6 Female 15.5 15.5 Right Neither 50 Right Some Regul NA <NA>
## 7 Male 18.0 19.0 Right L on R 54 Neither Some Regul NA <NA>
## 8 Male 22.0 21.5 Left R on L 55 Left Freq Never 200.00 Metric
## 9 Male 20.5 19.5 Right L on R 56 Right Freq Never 179.00 Metric
## 10 Male 19.8 20.0 Left L on R 59 Right Freq Never 180.00 Metric
## Age
## 1 23.667
## 2 17.417
## 3 18.667
## 4 21.333
## 5 30.750
## 6 18.500
## 7 17.750
## 8 18.500
## 9 17.417
## 10 17.417
|
|
## Sex Wr.Hnd NW.Hnd W.Hnd Fold Pulse Clap Exer Smoke Height M.I
## 1 Male 19.5 20.5 Left R on L 104 Left None Regul 177.8 Imperial
## 2 Female 19.0 18.5 Left L on R 104 Left Freq Never 170.0 Metric
## 3 Female 18.5 18.0 Left L on R 100 Neither Some Never 171.0 Metric
## 4 Male 21.0 20.4 Right L on R 100 Right Freq Heavy 184.0 Metric
## 5 Female 17.5 17.5 Right R on L 98 Left Freq Never NA <NA>
## 6 Male 17.5 17.0 Left L on R 97 Neither None Never 165.0 Metric
## 7 Male 22.5 23.0 Right R on L 96 Right None Never 170.0 Metric
## 8 Male 21.4 21.0 Right L on R 96 Neither Some Never 180.0 Metric
## 9 Female 17.5 17.8 Right R on L 96 Right Some Never NA <NA>
## 10 Female 18.5 18.0 Right R on L 92 Left Some Never 173.0 Metric
## 11 Female 18.0 17.7 Left R on L 92 Left Some Never NA <NA>
## 12 Female 18.5 18.0 Right R on L 92 Right Freq Never 172.0 Metric
## 13 Female 18.0 18.0 Right L on R 92 Neither Freq Never 165.0 Metric
## 14 Female 16.3 16.2 Right L on R 92 Right Some Regul 152.4 Imperial
## 15 Male 20.0 19.5 Right R on L 92 Right Some Never 179.1 Imperial
## Age
## 1 17.583
## 2 17.250
## 3 18.917
## 4 20.083
## 5 17.667
## 6 19.500
## 7 19.417
## 8 19.000
## 9 18.667
## 10 18.250
## 11 17.583
## 12 17.500
## 13 20.000
## 14 23.500
## 15 18.917
|
|
## Sex Wr.Hnd NW.Hnd W.Hnd Fold Pulse Clap Exer Smoke Height M.I
## 1 Female 18.3 18.5 Right R on L 75 Left Freq Never 170.00 Metric
## 2 Male 20.0 19.5 Right R on L 92 Right Some Never 179.10 Imperial
## 3 Female 17.5 18.0 Right R on L 68 Neither Freq Never 157.48 Imperial
## 4 Female 18.5 18.2 Right R on L 72 Neither Freq Never 167.64 Imperial
## 5 Female 20.5 20.5 Right R on L NA Left Freq Regul NA <NA>
## 6 Male 18.0 16.0 Right R on L NA Right Some Never 180.34 Imperial
## 7 Female 17.7 17.0 Right R on L 76 Right Some Never 167.00 Metric
## 8 Male 16.0 15.5 Right Neither 71 Right Freq Never 154.94 Imperial
## 9 Male 18.5 18.5 Right L on R NA Neither Freq Never 171.00 Metric
## 10 Male 20.5 19.5 Left L on R 80 Right Some Occas 182.88 Imperial
## Age
## 1 18.750
## 2 18.917
## 3 17.750
## 4 17.333
## 5 19.250
## 6 20.750
## 7 17.250
## 8 17.167
## 9 18.333
## 10 18.667
|
|
## Sex Wr.Hnd NW.Hnd W.Hnd Fold Pulse Clap Exer Smoke Height M.I
## 1 Female 17.0 17.0 Right L on R 79 Right Some Never 163.00 Metric
## 2 Female 17.7 17.0 Right R on L 76 Right Some Never 167.00 Metric
## 3 Female 16.7 15.1 Right Neither NA Right None Never 157.48 Imperial
## 4 Male 19.5 20.5 Left R on L 104 Left None Regul 177.80 Imperial
## 5 Male 19.5 20.2 Right R on L 60 Neither Freq Never 185.42 Imperial
## 6 Female 13.0 13.0 <NA> L on R 70 Left Freq Never 180.34 Imperial
## 7 Female 18.0 17.8 Right L on R 68 Right Some Never 168.90 Imperial
## 8 Male 20.5 20.0 Right L on R 68 Right Freq Never 190.00 Metric
## 9 Male 20.5 21.0 Right R on L 60 Right Freq Never 185.00 Metric
## 10 Female 19.5 19.2 Right R on L 70 Right Some Never 170.00 Metric
## 11 Female 16.2 16.4 Right R on L NA Right Freq Occas 172.00 Metric
## Age
## 1 24.667
## 2 17.250
## 3 18.167
## 4 17.583
## 5 32.667
## 6 17.417
## 7 17.083
## 8 17.500
## 9 17.917
## 10 18.167
## 11 17.000
3. relocate()
relocate: changes the order of the columns.
|
|
## [1] "Sex" "W.Hnd" "Fold" "Clap" "Exer" "Smoke" "M.I" "Wr.Hnd"
## [9] "NW.Hnd" "Pulse" "Height" "Age"
|
|
## [1] "Sex" "Wr.Hnd" "NW.Hnd" "W.Hnd" "Fold" "Clap" "Exer" "Smoke"
## [9] "Pulse" "Height" "M.I" "Age"
|
|
## [1] "Sex" "Wr.Hnd" "NW.Hnd" "W.Hnd" "Fold" "Clap" "Exer" "Smoke"
## [9] "Height" "M.I" "Age" "Pulse"
4. lag(), lead()
|
|
## [1] NA 1 2 3 4
|
|
## [1] NA NA 1 2 3
|
|
## [1] 0 1 2 3 4
|
|
## [1] 2 3 4 5 NA
|
|
## [1] 2 3 4 5 6
5. between(), near()
|
|
## [1] FALSE FALSE FALSE FALSE FALSE FALSE TRUE TRUE TRUE FALSE FALSE FALSE
|
|
## [1] FALSE
|
|
## [1] TRUE
6. coalesce()
๊ฐ ์์น๋ณ๋ก NA๊ฐ ์๋ ๊ฐ์ ์ฒซ๋ฒ์งธ ๊ฐ์ ๋ฐํ
|
|
## [1] 3
|
|
## [1] 0 2 0 3 1 4 0 5
|
|
## [1] 1 2 3 4 5
|
|
## [1] 1 2 3 4 5
7. recode()
case_when์ ํน์ํ ํํ๋ก์ ๋ฐ์ดํฐ๋ฅผ ๊ต์ฒดํ ๋ ์ฌ์ฉํ ์ ์์ ๊ฒ์ด๋ค.
|
|
## [1] "b" "b" "Apple" "Apple" "b" "b" "Apple" "b" "Apple"
## [10] "c"
|
|
## [1] "Banana" "Banana" "Apple" "Apple" "Banana" "Banana" "Apple" "Banana"
## [9] "Apple" "c"
|
|
## [1] "Banana" "Banana" "Apple" "Apple" "Banana" "Banana" "Apple" "Banana"
## [9] "Apple" NA
|
|
## [1] 3 5 3 5 2 3 3 2 5 3
|
|
## [1] "banana" "banana" "apple" "apple" "banana" "banana" "apple" "banana"
## [9] "apple" "carrot"
8. first(), last(), nth()
์ฒซ๋ฒ์งธ, ๋ง์ง๋ง ๋๋ ํน์ ์์น์ ์๋ ์์๋ฅผ ๋ฐํํ๋ ํจ์์ด๋ค.
|
|
## [1] 1
|
|
## [1] 1
|
|
## [1] 3
|
|
## [1] 7
9. rownames_to_column(), column_to_rownames()
|
|
## C Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1 1 5.1 3.5 1.4 0.2 setosa
## 2 2 4.9 3.0 1.4 0.2 setosa
## 3 3 4.7 3.2 1.3 0.2 setosa
## 4 4 4.6 3.1 1.5 0.2 setosa
## 5 5 5.0 3.6 1.4 0.2 setosa
## 6 6 5.4 3.9 1.7 0.4 setosa
|
|
## Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1 5.1 3.5 1.4 0.2 setosa
## 2 4.9 3.0 1.4 0.2 setosa
## 3 4.7 3.2 1.3 0.2 setosa
## 4 4.6 3.1 1.5 0.2 setosa
## 5 5.0 3.6 1.4 0.2 setosa
## 6 5.4 3.9 1.7 0.4 setosa
10. bind_rows(), bind_cols()
๊ธฐ์กด์ rbind๋ cbind ๋์ ์ ํ์ฉํ๋ฉด ๋ ๊ฒ ๊ฐ๋ค.
|
|
## # A tibble: 8 x 14
## name height mass hair_color skin_color eye_color birth_year sex gender
## <chr> <int> <dbl> <chr> <chr> <chr> <dbl> <chr> <chr>
## 1 Luke~ 172 77 blond fair blue 19 male mascu~
## 2 C-3PO 167 75 <NA> gold yellow 112 none mascu~
## 3 R2-D2 96 32 <NA> white, bl~ red 33 none mascu~
## 4 Dart~ 202 136 none white yellow 41.9 male mascu~
## 5 Bigg~ 183 84 black light brown 24 male mascu~
## 6 Obi-~ 182 77 auburn, w~ fair blue-gray 57 male mascu~
## 7 Anak~ 188 84 blond fair blue 41.9 male mascu~
## 8 Wilh~ 180 NA auburn, g~ fair blue 64 male mascu~
## # ... with 5 more variables: homeworld <chr>, species <chr>, films <list>,
## # vehicles <list>, starships <list>
|
|
## # A tibble: 8 x 14
## name height mass hair_color skin_color eye_color birth_year sex gender
## <chr> <int> <dbl> <chr> <chr> <chr> <dbl> <chr> <chr>
## 1 Luke~ 172 77 blond fair blue 19 male mascu~
## 2 C-3PO 167 75 <NA> gold yellow 112 none mascu~
## 3 R2-D2 96 32 <NA> white, bl~ red 33 none mascu~
## 4 Dart~ 202 136 none white yellow 41.9 male mascu~
## 5 <NA> NA 84 <NA> <NA> <NA> NA <NA> <NA>
## 6 <NA> NA 77 <NA> <NA> <NA> NA <NA> <NA>
## 7 <NA> NA 84 <NA> <NA> <NA> NA <NA> <NA>
## 8 <NA> NA NA <NA> <NA> <NA> NA <NA> <NA>
## # ... with 5 more variables: homeworld <chr>, species <chr>, films <list>,
## # vehicles <list>, starships <list>
|
|
## # A tibble: 87 x 7
## name height mass hair_color birth_year sex gender
## <chr> <int> <dbl> <chr> <dbl> <chr> <chr>
## 1 Luke Skywalker 172 77 blond 19 male masculine
## 2 C-3PO 167 75 <NA> 112 none masculine
## 3 R2-D2 96 32 <NA> 33 none masculine
## 4 Darth Vader 202 136 none 41.9 male masculine
## 5 Leia Organa 150 49 brown 19 female feminine
## 6 Owen Lars 178 120 brown, grey 52 male masculine
## 7 Beru Whitesun lars 165 75 brown 47 female feminine
## 8 R5-D4 97 32 <NA> NA none masculine
## 9 Biggs Darklighter 183 84 black 24 male masculine
## 10 Obi-Wan Kenobi 182 77 auburn, white 57 male masculine
## # ... with 77 more rows
|
|
11. mutate_all, mutate_if
๋ชจ๋ ๋ณ์๋ฅผ ๋ค ํน์ ํจ์๋ฅผ ๊ฑฐ์น ํํ๋ก ๋ฐ๊พธ๊ฑฐ๋, ์กฐ๊ฑด์ ์ฃผ์ด์ ํน์ ํจ์๋ฅผ ๊ฑฐ์น ํํ๋ก ๋ฐ๊พธ๋ ํจ์์ด๋ค.
|
|
## Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1 5 3 1 0 1
## 2 4 3 1 0 1
## 3 4 3 1 0 1
## 4 4 3 1 0 1
## 5 5 3 1 0 1
## 6 5 3 1 0 1
|
|
## Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1 5 3 1 0 setosa
## 2 4 3 1 0 setosa
## 3 4 3 1 0 setosa
## 4 4 3 1 0 setosa
## 5 5 3 1 0 setosa
## 6 5 3 1 0 setosa
12. inner_join, left_join, right_join, full_join
SQL์์ ๋ดค๋ join ํจ์๊ฐ ์ญ์๋ ๊ทธ๋๋ก ์๋ค. ๊ทธ๋ฐ๋ฐ ์น์ ํ๊ฒ ๋ฌด์์ ๊ธฐ์ค์ผ๋ก join ํ๋์ง๋ ์์ ์๋ ค์ค์ ์ข์ ๊ฒ ๊ฐ๋ค.
|
|
## # A tibble: 3 x 2
## name band
## <chr> <chr>
## 1 Mick Stones
## 2 John Beatles
## 3 Paul Beatles
|
|
## # A tibble: 3 x 2
## name plays
## <chr> <chr>
## 1 John guitar
## 2 Paul bass
## 3 Keith guitar
|
|
## Joining, by = "name"
## # A tibble: 2 x 3
## name band plays
## <chr> <chr> <chr>
## 1 John Beatles guitar
## 2 Paul Beatles bass
|
|
## Joining, by = "name"
## # A tibble: 3 x 3
## name band plays
## <chr> <chr> <chr>
## 1 Mick Stones <NA>
## 2 John Beatles guitar
## 3 Paul Beatles bass
|
|
## Joining, by = "name"
## # A tibble: 3 x 3
## name band plays
## <chr> <chr> <chr>
## 1 John Beatles guitar
## 2 Paul Beatles bass
## 3 Keith <NA> guitar
|
|
## Joining, by = "name"
## # A tibble: 4 x 3
## name band plays
## <chr> <chr> <chr>
## 1 Mick Stones <NA>
## 2 John Beatles guitar
## 3 Paul Beatles bass
## 4 Keith <NA> guitar
13. semi_join, anti_join
|
|
## Joining, by = "name"
## # A tibble: 2 x 3
## name band plays
## <chr> <chr> <chr>
## 1 John Beatles guitar
## 2 Paul Beatles bass
|
|
## Joining, by = "name"
## # A tibble: 2 x 2
## name band
## <chr> <chr>
## 1 John Beatles
## 2 Paul Beatles
|
|
## Joining, by = "name"
## # A tibble: 1 x 2
## name band
## <chr> <chr>
## 1 Mick Stones
์ฐธ๊ณ
[1] slice์ relocate ์์๋ slack ์ฌ๊ธฐ๋ก์ดํต๊ณ์ํ์ ์ฐธ๊ณ ํ์์ต๋๋ค.