Tugas Akhir Praktikum Mandarel
A. Contoh Akses Database
Ketika akan mengakses database di R, diperlukan package DBI dan RSQlite, sehingga perlu dipastikan bahwa kedua package tersebut telah terinstall.
install.packages(c("RSQLite", "DBI"), dependencies = TRUE)Kemudian panggil package tersebut dengan fungsi library().
library(RSQLite)
library(DBI)1. Menggunakan Engine SQL
Perlu dibuat suatu objek connection sebagai variabel yang menghubungkan SQL dengan aplikasi R Untuk menggunakan SQL engine di R.
datasql <- DBI::dbConnect(RSQLite::SQLite(), "C:/sqlite/Chinook.db")Syntax menunjukkan pembuatan variabel datasql sebagai objek connection dengan engine SQL pada file tertentu yang merujuk ke device pengguna.
Pengguna dapat menggunakan syntax SQL di R seperti contoh berikut:
SELECT
*
FROM
Tracks
WHERE
AlbumId==1;| TrackId | Name | AlbumId | MediaTypeId | GenreId | Composer | Milliseconds | Bytes | UnitPrice |
|---|---|---|---|---|---|---|---|---|
| 1 | For Those About To Rock (We Salute You) | 1 | 1 | 1 | Angus Young, Malcolm Young, Brian Johnson | 343719 | 11170334 | 0.99 |
| 6 | Put The Finger On You | 1 | 1 | 1 | Angus Young, Malcolm Young, Brian Johnson | 205662 | 6713451 | 0.99 |
| 7 | Let’s Get It Up | 1 | 1 | 1 | Angus Young, Malcolm Young, Brian Johnson | 233926 | 7636561 | 0.99 |
| 8 | Inject The Venom | 1 | 1 | 1 | Angus Young, Malcolm Young, Brian Johnson | 210834 | 6852860 | 0.99 |
| 9 | Snowballed | 1 | 1 | 1 | Angus Young, Malcolm Young, Brian Johnson | 203102 | 6599424 | 0.99 |
| 10 | Evil Walks | 1 | 1 | 1 | Angus Young, Malcolm Young, Brian Johnson | 263497 | 8611245 | 0.99 |
| 11 | C.O.D. | 1 | 1 | 1 | Angus Young, Malcolm Young, Brian Johnson | 199836 | 6566314 | 0.99 |
| 12 | Breaking The Rules | 1 | 1 | 1 | Angus Young, Malcolm Young, Brian Johnson | 263288 | 8596840 | 0.99 |
| 13 | Night Of The Long Knives | 1 | 1 | 1 | Angus Young, Malcolm Young, Brian Johnson | 205688 | 6706347 | 0.99 |
| 14 | Spellbound | 1 | 1 | 1 | Angus Young, Malcolm Young, Brian Johnson | 270863 | 8817038 | 0.99 |
2. Menggunakan DPLYR
Diperlukan variabel baru untuk memanggil suatu database.
datadplyr <- DBI::dbConnect(RSQLite::SQLite(), "C:/sqlite/Chinook.db")dengan kelas:
class(datadplyr)## [1] "SQLiteConnection"
## attr(,"package")
## [1] "RSQLite"
Untuk melihat ada tabel apa saja pada chinook:
RSQLite::dbListTables(datadplyr)## [1] "albums" "artists" "customers" "employees"
## [5] "genres" "invoice_items" "invoices" "media_types"
## [9] "playlist_track" "playlists" "sqlite_sequence" "sqlite_stat1"
## [13] "tracks"
Panggil package yang akan digunakan terlebih dahulu, yaitu package dplry dan dbplyr atau hanya memanggil package tidyverse yang mencakup keduanya sekaligus.
library(dplyr)
library(dbplyr)Dengan menggunakan fungsi dplyr, kita dapat memanggil tabel spesifik dari database chinook2 sebagai variabel baru di R:
tracks <- dplyr::tbl(datadplyr, "Tracks")
tracks## # Source: table<Tracks> [?? x 9]
## # Database: sqlite 3.37.0 [C:\sqlite\chinook.db]
## TrackId Name AlbumId MediaTypeId GenreId Composer Milliseconds Bytes
## <int> <chr> <int> <int> <int> <chr> <int> <int>
## 1 1 For Tho~ 1 1 1 Angus Young~ 343719 1.12e7
## 2 2 Balls t~ 2 2 1 <NA> 342562 5.51e6
## 3 3 Fast As~ 3 2 1 F. Baltes, ~ 230619 3.99e6
## 4 4 Restles~ 3 2 1 F. Baltes, ~ 252051 4.33e6
## 5 5 Princes~ 3 2 1 Deaffy & R.~ 375418 6.29e6
## 6 6 Put The~ 1 1 1 Angus Young~ 205662 6.71e6
## 7 7 Let's G~ 1 1 1 Angus Young~ 233926 7.64e6
## 8 8 Inject ~ 1 1 1 Angus Young~ 210834 6.85e6
## 9 9 Snowbal~ 1 1 1 Angus Young~ 203102 6.60e6
## 10 10 Evil Wa~ 1 1 1 Angus Young~ 263497 8.61e6
## # ... with more rows, and 1 more variable: UnitPrice <dbl>
Dalam syntax dplyr, fungsi dalam kurung dapat diubah menjadi “%>%” agar lebih mudah terbaca. Contoh penggunaannya :
datadplyr2 <- tracks %>%
select(Name, AlbumId, GenreId, UnitPrice) %>%
filter(AlbumId==1) %>%
arrange(UnitPrice)
datadplyr2## # Source: lazy query [?? x 4]
## # Database: sqlite 3.37.0 [C:\sqlite\chinook.db]
## # Ordered by: UnitPrice
## Name AlbumId GenreId UnitPrice
## <chr> <int> <int> <dbl>
## 1 For Those About To Rock (We Salute You) 1 1 0.99
## 2 Put The Finger On You 1 1 0.99
## 3 Let's Get It Up 1 1 0.99
## 4 Inject The Venom 1 1 0.99
## 5 Snowballed 1 1 0.99
## 6 Evil Walks 1 1 0.99
## 7 C.O.D. 1 1 0.99
## 8 Breaking The Rules 1 1 0.99
## 9 Night Of The Long Knives 1 1 0.99
## 10 Spellbound 1 1 0.99
Syntax di atas memanggil tabel tracks dan mencetak nama track, nomor ID album, nomor ID genre, dan harga satu unit dengan nomor ID album adalah 1 lalu diurutkan berdasarkan harga satu unit.
Pengguna dapat menggunakan syntax berikut untuk melihat bagaimana jika fungsi tersebut dijalankan di SQL:
dplyr::show_query(datadplyr2)## <SQL>
## SELECT *
## FROM (SELECT `Name`, `AlbumId`, `GenreId`, `UnitPrice`
## FROM `Tracks`)
## WHERE (`AlbumId` = 1.0)
## ORDER BY `UnitPrice`
B. Data Wrangling
Pada tahap ini kita akan menggunakan dataset attenu yang telah tersedia pada default datasets di R.
library(datasets)
attenuSelect
Perintah select berfungsi untuk memilih kolom tertentu saja untuk dicetak.
attenu %>% select(event, mag, station, accel)## event mag station accel
## 1 1 7.0 117 0.359
## 2 2 7.4 1083 0.014
## 3 2 7.4 1095 0.196
## 4 2 7.4 283 0.135
## 5 2 7.4 135 0.062
## 6 2 7.4 475 0.054
## 7 2 7.4 113 0.014
## 8 2 7.4 1008 0.018
## 9 2 7.4 1028 0.010
## 10 2 7.4 2001 0.004
## 11 2 7.4 117 0.004
## 12 3 5.3 1117 0.127
## 13 4 6.1 1438 0.411
## 14 4 6.1 1083 0.018
## 15 4 6.1 1013 0.509
## 16 4 6.1 1014 0.467
## 17 4 6.1 1015 0.279
## 18 4 6.1 1016 0.072
## 19 4 6.1 1095 0.012
## 20 4 6.1 1011 0.006
## 21 4 6.1 1028 0.003
## 22 5 6.6 270 0.018
## 23 5 6.6 280 0.048
## 24 5 6.6 116 0.011
## 25 5 6.6 266 0.007
## 26 5 6.6 117 0.142
## 27 5 6.6 113 0.031
## 28 5 6.6 112 0.006
## 29 5 6.6 130 0.010
## 30 5 6.6 475 0.010
## 31 5 6.6 269 0.006
## 32 5 6.6 135 0.013
## 33 6 5.6 1093 0.005
## 34 7 5.7 1093 0.003
## 35 8 5.3 111 0.086
## 36 8 5.3 116 0.179
## 37 8 5.3 290 0.205
## 38 8 5.3 112 0.073
## 39 8 5.3 113 0.045
## 40 9 6.6 128 0.374
## 41 9 6.6 126 0.200
## 42 9 6.6 127 0.147
## 43 9 6.6 141 0.188
## 44 9 6.6 266 0.204
## 45 9 6.6 110 0.335
## 46 9 6.6 1027 0.057
## 47 9 6.6 111 0.021
## 48 9 6.6 125 0.152
## 49 9 6.6 135 0.217
## 50 9 6.6 475 0.114
## 51 9 6.6 262 0.150
## 52 9 6.6 269 0.148
## 53 9 6.6 1052 0.112
## 54 9 6.6 411 0.043
## 55 9 6.6 290 0.057
## 56 9 6.6 130 0.030
## 57 9 6.6 272 0.027
## 58 9 6.6 1096 0.028
## 59 9 6.6 1102 0.034
## 60 9 6.6 112 0.030
## 61 9 6.6 113 0.039
## 62 10 5.3 1028 0.030
## 63 11 7.7 2714 0.110
## 64 11 7.7 2708 0.010
## 65 11 7.7 2715 0.010
## 66 12 6.2 3501 0.390
## 67 13 5.6 655 0.031
## 68 13 5.6 272 0.130
## 69 14 5.2 1032 0.011
## 70 14 5.2 1377 0.120
## 71 14 5.2 1028 0.170
## 72 14 5.2 1250 0.140
## 73 15 6.0 1051 0.110
## 74 15 6.0 1293 0.040
## 75 15 6.0 1291 0.070
## 76 15 6.0 1292 0.080
## 77 16 5.1 283 0.210
## 78 16 5.1 885 0.390
## 79 16 5.1 <NA> 0.280
## 80 17 7.6 2734 0.160
## 81 17 7.6 <NA> 0.064
## 82 17 7.6 2728 0.090
## 83 18 5.8 1413 0.420
## 84 18 5.8 1445 0.230
## 85 18 5.8 1408 0.130
## 86 18 5.8 1411 0.260
## 87 18 5.8 1410 0.270
## 88 18 5.8 1409 0.260
## 89 18 5.8 1377 0.110
## 90 18 5.8 1492 0.120
## 91 18 5.8 1251 0.038
## 92 18 5.8 1422 0.044
## 93 18 5.8 1376 0.046
## 94 19 6.5 <NA> 0.170
## 95 19 6.5 286 0.210
## 96 19 6.5 <NA> 0.320
## 97 19 6.5 5028 0.520
## 98 19 6.5 942 0.720
## 99 19 6.5 <NA> 0.320
## 100 19 6.5 5054 0.810
## 101 19 6.5 958 0.640
## 102 19 6.5 952 0.560
## 103 19 6.5 5165 0.510
## 104 19 6.5 117 0.400
## 105 19 6.5 955 0.610
## 106 19 6.5 5055 0.260
## 107 19 6.5 <NA> 0.240
## 108 19 6.5 <NA> 0.460
## 109 19 6.5 5060 0.220
## 110 19 6.5 412 0.230
## 111 19 6.5 5053 0.280
## 112 19 6.5 5058 0.380
## 113 19 6.5 5057 0.270
## 114 19 6.5 <NA> 0.310
## 115 19 6.5 5051 0.200
## 116 19 6.5 <NA> 0.110
## 117 19 6.5 5115 0.430
## 118 19 6.5 <NA> 0.270
## 119 19 6.5 931 0.150
## 120 19 6.5 5056 0.150
## 121 19 6.5 5059 0.150
## 122 19 6.5 5061 0.130
## 123 19 6.5 <NA> 0.190
## 124 19 6.5 5062 0.130
## 125 19 6.5 5052 0.066
## 126 19 6.5 <NA> 0.350
## 127 19 6.5 724 0.100
## 128 19 6.5 <NA> 0.160
## 129 19 6.5 5066 0.140
## 130 19 6.5 5050 0.049
## 131 19 6.5 2316 0.034
## 132 20 5.0 5055 0.264
## 133 20 5.0 942 0.263
## 134 20 5.0 5028 0.230
## 135 20 5.0 5165 0.147
## 136 20 5.0 952 0.286
## 137 20 5.0 958 0.157
## 138 20 5.0 955 0.237
## 139 20 5.0 117 0.133
## 140 20 5.0 412 0.055
## 141 20 5.0 5053 0.097
## 142 20 5.0 5054 0.129
## 143 20 5.0 5058 0.192
## 144 20 5.0 5057 0.147
## 145 20 5.0 5115 0.154
## 146 20 5.0 5056 0.060
## 147 20 5.0 5060 0.057
## 148 21 5.8 1030 0.120
## 149 21 5.8 1418 0.154
## 150 21 5.8 1383 0.052
## 151 21 5.8 1308 0.045
## 152 21 5.8 1298 0.086
## 153 21 5.8 1299 0.056
## 154 21 5.8 1219 0.065
## 155 22 5.5 <NA> 0.259
## 156 22 5.5 <NA> 0.267
## 157 22 5.5 1030 0.071
## 158 22 5.5 1418 0.275
## 159 22 5.5 1383 0.058
## 160 22 5.5 <NA> 0.026
## 161 22 5.5 1299 0.039
## 162 22 5.5 1308 0.112
## 163 22 5.5 1219 0.065
## 164 22 5.5 1456 0.026
## 165 23 5.3 5045 0.123
## 166 23 5.3 5044 0.133
## 167 23 5.3 5160 0.073
## 168 23 5.3 5043 0.097
## 169 23 5.3 5047 0.096
## 170 23 5.3 c168 0.230
## 171 23 5.3 5068 0.082
## 172 23 5.3 c118 0.110
## 173 23 5.3 5042 0.110
## 174 23 5.3 5067 0.094
## 175 23 5.3 5049 0.040
## 176 23 5.3 c204 0.050
## 177 23 5.3 5070 0.022
## 178 23 5.3 c266 0.070
## 179 23 5.3 c203 0.080
## 180 23 5.3 5069 0.033
## 181 23 5.3 5073 0.017
## 182 23 5.3 5072 0.022
Arrange
Perintah arrange berfungsi untuk mengurutkan tabel berdasarkan kolom tertentu yang telah ditentukan. Default dari perintah ini adalah pengurutan dari yang terkecil. Kita bisa menggunakan fungsi desc untuk mengurutkannya dari yang terbesar.
attenu %>% arrange(accel)## event mag station dist accel
## 1 4 6.1 1028 123.0 0.003
## 2 7 5.7 1093 62.0 0.003
## 3 2 7.4 2001 359.0 0.004
## 4 2 7.4 117 370.0 0.004
## 5 6 5.6 1093 62.0 0.005
## 6 4 6.1 1011 112.0 0.006
## 7 5 6.6 112 147.0 0.006
## 8 5 6.6 269 203.0 0.006
## 9 5 6.6 266 200.0 0.007
## 10 2 7.4 1028 293.0 0.010
## 11 5 6.6 130 187.0 0.010
## 12 5 6.6 475 197.0 0.010
## 13 11 7.7 2708 145.0 0.010
## 14 11 7.7 2715 300.0 0.010
## 15 5 6.6 116 141.0 0.011
## 16 14 5.2 1032 17.0 0.011
## 17 4 6.1 1095 105.0 0.012
## 18 5 6.6 135 211.0 0.013
## 19 2 7.4 1083 148.0 0.014
## 20 2 7.4 113 156.0 0.014
## 21 23 5.3 5073 49.2 0.017
## 22 2 7.4 1008 224.0 0.018
## 23 4 6.1 1083 63.6 0.018
## 24 5 6.6 270 105.0 0.018
## 25 9 6.6 111 87.0 0.021
## 26 23 5.3 5070 44.4 0.022
## 27 23 5.3 5072 53.1 0.022
## 28 22 5.5 <NA> 26.5 0.026
## 29 22 5.5 1456 48.3 0.026
## 30 9 6.6 272 62.0 0.027
## 31 9 6.6 1096 64.0 0.028
## 32 9 6.6 130 61.4 0.030
## 33 9 6.6 112 88.0 0.030
## 34 10 5.3 1028 31.0 0.030
## 35 5 6.6 113 130.0 0.031
## 36 13 5.6 655 50.0 0.031
## 37 23 5.3 5069 47.7 0.033
## 38 9 6.6 1102 82.0 0.034
## 39 19 6.5 2316 64.0 0.034
## 40 18 5.8 1251 23.4 0.038
## 41 9 6.6 113 91.0 0.039
## 42 22 5.5 1299 29.0 0.039
## 43 15 6.0 1293 32.0 0.040
## 44 23 5.3 5049 41.4 0.040
## 45 9 6.6 411 56.9 0.043
## 46 18 5.8 1422 30.0 0.044
## 47 8 5.3 113 29.0 0.045
## 48 21 5.8 1308 20.8 0.045
## 49 18 5.8 1376 38.9 0.046
## 50 5 6.6 280 122.0 0.048
## 51 19 6.5 5050 60.0 0.049
## 52 23 5.3 c204 43.6 0.050
## 53 21 5.8 1383 16.7 0.052
## 54 2 7.4 475 109.0 0.054
## 55 20 5.0 412 12.0 0.055
## 56 21 5.8 1299 33.1 0.056
## 57 9 6.6 1027 66.0 0.057
## 58 9 6.6 290 60.7 0.057
## 59 20 5.0 5060 25.0 0.057
## 60 22 5.5 1383 22.5 0.058
## 61 20 5.0 5056 23.9 0.060
## 62 2 7.4 135 107.0 0.062
## 63 17 7.6 <NA> 32.9 0.064
## 64 21 5.8 1219 40.3 0.065
## 65 22 5.5 1219 37.8 0.065
## 66 19 6.5 5052 32.0 0.066
## 67 15 6.0 1291 30.0 0.070
## 68 23 5.3 c266 46.1 0.070
## 69 22 5.5 1030 11.1 0.071
## 70 4 6.1 1016 17.3 0.072
## 71 8 5.3 112 22.0 0.073
## 72 23 5.3 5160 12.1 0.073
## 73 15 6.0 1292 31.0 0.080
## 74 23 5.3 c203 47.1 0.080
## 75 23 5.3 5068 35.9 0.082
## 76 8 5.3 111 19.0 0.086
## 77 21 5.8 1298 28.5 0.086
## 78 17 7.6 2728 92.2 0.090
## 79 23 5.3 5067 38.5 0.094
## 80 23 5.3 5047 20.5 0.096
## 81 20 5.0 5053 12.2 0.097
## 82 23 5.3 5043 20.5 0.097
## 83 19 6.5 724 36.0 0.100
## 84 11 7.7 2714 45.0 0.110
## 85 15 6.0 1051 8.0 0.110
## 86 18 5.8 1377 17.9 0.110
## 87 19 6.5 <NA> 15.0 0.110
## 88 23 5.3 c118 36.1 0.110
## 89 23 5.3 5042 36.3 0.110
## 90 9 6.6 1052 46.7 0.112
## 91 22 5.5 1308 30.9 0.112
## 92 9 6.6 475 25.7 0.114
## 93 14 5.2 1377 8.0 0.120
## 94 18 5.8 1492 19.2 0.120
## 95 21 5.8 1030 10.8 0.120
## 96 23 5.3 5045 5.8 0.123
## 97 3 5.3 1117 8.0 0.127
## 98 20 5.0 5054 12.8 0.129
## 99 13 5.6 272 16.0 0.130
## 100 18 5.8 1408 9.1 0.130
## 101 19 6.5 5061 23.0 0.130
## 102 19 6.5 5062 29.0 0.130
## 103 20 5.0 117 10.5 0.133
## 104 23 5.3 5044 12.0 0.133
## 105 2 7.4 283 85.0 0.135
## 106 14 5.2 1250 10.0 0.140
## 107 19 6.5 5066 49.0 0.140
## 108 5 6.6 117 45.0 0.142
## 109 9 6.6 127 20.2 0.147
## 110 20 5.0 5165 9.4 0.147
## 111 20 5.0 5057 14.9 0.147
## 112 9 6.6 269 37.4 0.148
## 113 9 6.6 262 28.6 0.150
## 114 19 6.5 931 18.0 0.150
## 115 19 6.5 5056 22.0 0.150
## 116 19 6.5 5059 22.0 0.150
## 117 9 6.6 125 23.4 0.152
## 118 20 5.0 5115 17.6 0.154
## 119 21 5.8 1418 15.7 0.154
## 120 20 5.0 958 9.7 0.157
## 121 17 7.6 2734 25.4 0.160
## 122 19 6.5 <NA> 43.5 0.160
## 123 14 5.2 1028 10.0 0.170
## 124 19 6.5 <NA> 23.5 0.170
## 125 8 5.3 116 21.0 0.179
## 126 9 6.6 141 21.1 0.188
## 127 19 6.5 <NA> 23.2 0.190
## 128 20 5.0 5058 14.6 0.192
## 129 2 7.4 1095 42.0 0.196
## 130 9 6.6 126 19.6 0.200
## 131 19 6.5 5051 14.0 0.200
## 132 9 6.6 266 21.9 0.204
## 133 8 5.3 290 13.0 0.205
## 134 16 5.1 283 2.9 0.210
## 135 19 6.5 286 26.0 0.210
## 136 9 6.6 135 24.6 0.217
## 137 19 6.5 5060 8.5 0.220
## 138 18 5.8 1445 1.6 0.230
## 139 19 6.5 412 8.5 0.230
## 140 20 5.0 5028 8.9 0.230
## 141 23 5.3 c168 25.3 0.230
## 142 20 5.0 955 10.5 0.237
## 143 19 6.5 <NA> 7.6 0.240
## 144 22 5.5 <NA> 4.0 0.259
## 145 18 5.8 1411 3.7 0.260
## 146 18 5.8 1409 7.4 0.260
## 147 19 6.5 5055 7.5 0.260
## 148 20 5.0 942 8.8 0.263
## 149 20 5.0 5055 7.5 0.264
## 150 22 5.5 <NA> 10.1 0.267
## 151 18 5.8 1410 5.3 0.270
## 152 19 6.5 5057 12.7 0.270
## 153 19 6.5 <NA> 17.7 0.270
## 154 22 5.5 1418 17.7 0.275
## 155 4 6.1 1015 13.0 0.279
## 156 16 5.1 <NA> 7.6 0.280
## 157 19 6.5 5053 10.6 0.280
## 158 20 5.0 952 9.7 0.286
## 159 19 6.5 <NA> 12.9 0.310
## 160 19 6.5 <NA> 0.5 0.320
## 161 19 6.5 <NA> 1.4 0.320
## 162 9 6.6 110 24.2 0.335
## 163 19 6.5 <NA> 32.7 0.350
## 164 1 7.0 117 12.0 0.359
## 165 9 6.6 128 17.0 0.374
## 166 19 6.5 5058 12.6 0.380
## 167 12 6.2 3501 5.0 0.390
## 168 16 5.1 885 3.2 0.390
## 169 19 6.5 117 6.2 0.400
## 170 4 6.1 1438 16.1 0.411
## 171 18 5.8 1413 1.2 0.420
## 172 19 6.5 5115 16.0 0.430
## 173 19 6.5 <NA> 8.4 0.460
## 174 4 6.1 1014 9.3 0.467
## 175 4 6.1 1013 6.6 0.509
## 176 19 6.5 5165 5.1 0.510
## 177 19 6.5 5028 0.6 0.520
## 178 19 6.5 952 4.0 0.560
## 179 19 6.5 955 6.8 0.610
## 180 19 6.5 958 3.8 0.640
## 181 19 6.5 942 1.3 0.720
## 182 19 6.5 5054 2.6 0.810
Filter
Perintah filter berfungsi untuk menyeleksi atau memilih baris berdasarkan ketentuan tertentu.
attenu %>% filter(mag>7.2)## event mag station dist accel
## 1 2 7.4 1083 148.0 0.014
## 2 2 7.4 1095 42.0 0.196
## 3 2 7.4 283 85.0 0.135
## 4 2 7.4 135 107.0 0.062
## 5 2 7.4 475 109.0 0.054
## 6 2 7.4 113 156.0 0.014
## 7 2 7.4 1008 224.0 0.018
## 8 2 7.4 1028 293.0 0.010
## 9 2 7.4 2001 359.0 0.004
## 10 2 7.4 117 370.0 0.004
## 11 11 7.7 2714 45.0 0.110
## 12 11 7.7 2708 145.0 0.010
## 13 11 7.7 2715 300.0 0.010
## 14 17 7.6 2734 25.4 0.160
## 15 17 7.6 <NA> 32.9 0.064
## 16 17 7.6 2728 92.2 0.090
Mutate
mutate adalah perintah yang dapat membuat kolom baru dari kolom yang sudah ada.
attenu %>% mutate(earthquakes=mag/event)## event mag station dist accel earthquakes
## 1 1 7.0 117 12.0 0.359 7.0000000
## 2 2 7.4 1083 148.0 0.014 3.7000000
## 3 2 7.4 1095 42.0 0.196 3.7000000
## 4 2 7.4 283 85.0 0.135 3.7000000
## 5 2 7.4 135 107.0 0.062 3.7000000
## 6 2 7.4 475 109.0 0.054 3.7000000
## 7 2 7.4 113 156.0 0.014 3.7000000
## 8 2 7.4 1008 224.0 0.018 3.7000000
## 9 2 7.4 1028 293.0 0.010 3.7000000
## 10 2 7.4 2001 359.0 0.004 3.7000000
## 11 2 7.4 117 370.0 0.004 3.7000000
## 12 3 5.3 1117 8.0 0.127 1.7666667
## 13 4 6.1 1438 16.1 0.411 1.5250000
## 14 4 6.1 1083 63.6 0.018 1.5250000
## 15 4 6.1 1013 6.6 0.509 1.5250000
## 16 4 6.1 1014 9.3 0.467 1.5250000
## 17 4 6.1 1015 13.0 0.279 1.5250000
## 18 4 6.1 1016 17.3 0.072 1.5250000
## 19 4 6.1 1095 105.0 0.012 1.5250000
## 20 4 6.1 1011 112.0 0.006 1.5250000
## 21 4 6.1 1028 123.0 0.003 1.5250000
## 22 5 6.6 270 105.0 0.018 1.3200000
## 23 5 6.6 280 122.0 0.048 1.3200000
## 24 5 6.6 116 141.0 0.011 1.3200000
## 25 5 6.6 266 200.0 0.007 1.3200000
## 26 5 6.6 117 45.0 0.142 1.3200000
## 27 5 6.6 113 130.0 0.031 1.3200000
## 28 5 6.6 112 147.0 0.006 1.3200000
## 29 5 6.6 130 187.0 0.010 1.3200000
## 30 5 6.6 475 197.0 0.010 1.3200000
## 31 5 6.6 269 203.0 0.006 1.3200000
## 32 5 6.6 135 211.0 0.013 1.3200000
## 33 6 5.6 1093 62.0 0.005 0.9333333
## 34 7 5.7 1093 62.0 0.003 0.8142857
## 35 8 5.3 111 19.0 0.086 0.6625000
## 36 8 5.3 116 21.0 0.179 0.6625000
## 37 8 5.3 290 13.0 0.205 0.6625000
## 38 8 5.3 112 22.0 0.073 0.6625000
## 39 8 5.3 113 29.0 0.045 0.6625000
## 40 9 6.6 128 17.0 0.374 0.7333333
## 41 9 6.6 126 19.6 0.200 0.7333333
## 42 9 6.6 127 20.2 0.147 0.7333333
## 43 9 6.6 141 21.1 0.188 0.7333333
## 44 9 6.6 266 21.9 0.204 0.7333333
## 45 9 6.6 110 24.2 0.335 0.7333333
## 46 9 6.6 1027 66.0 0.057 0.7333333
## 47 9 6.6 111 87.0 0.021 0.7333333
## 48 9 6.6 125 23.4 0.152 0.7333333
## 49 9 6.6 135 24.6 0.217 0.7333333
## 50 9 6.6 475 25.7 0.114 0.7333333
## 51 9 6.6 262 28.6 0.150 0.7333333
## 52 9 6.6 269 37.4 0.148 0.7333333
## 53 9 6.6 1052 46.7 0.112 0.7333333
## 54 9 6.6 411 56.9 0.043 0.7333333
## 55 9 6.6 290 60.7 0.057 0.7333333
## 56 9 6.6 130 61.4 0.030 0.7333333
## 57 9 6.6 272 62.0 0.027 0.7333333
## 58 9 6.6 1096 64.0 0.028 0.7333333
## 59 9 6.6 1102 82.0 0.034 0.7333333
## 60 9 6.6 112 88.0 0.030 0.7333333
## 61 9 6.6 113 91.0 0.039 0.7333333
## 62 10 5.3 1028 31.0 0.030 0.5300000
## 63 11 7.7 2714 45.0 0.110 0.7000000
## 64 11 7.7 2708 145.0 0.010 0.7000000
## 65 11 7.7 2715 300.0 0.010 0.7000000
## 66 12 6.2 3501 5.0 0.390 0.5166667
## 67 13 5.6 655 50.0 0.031 0.4307692
## 68 13 5.6 272 16.0 0.130 0.4307692
## 69 14 5.2 1032 17.0 0.011 0.3714286
## 70 14 5.2 1377 8.0 0.120 0.3714286
## 71 14 5.2 1028 10.0 0.170 0.3714286
## 72 14 5.2 1250 10.0 0.140 0.3714286
## 73 15 6.0 1051 8.0 0.110 0.4000000
## 74 15 6.0 1293 32.0 0.040 0.4000000
## 75 15 6.0 1291 30.0 0.070 0.4000000
## 76 15 6.0 1292 31.0 0.080 0.4000000
## 77 16 5.1 283 2.9 0.210 0.3187500
## 78 16 5.1 885 3.2 0.390 0.3187500
## 79 16 5.1 <NA> 7.6 0.280 0.3187500
## 80 17 7.6 2734 25.4 0.160 0.4470588
## 81 17 7.6 <NA> 32.9 0.064 0.4470588
## 82 17 7.6 2728 92.2 0.090 0.4470588
## 83 18 5.8 1413 1.2 0.420 0.3222222
## 84 18 5.8 1445 1.6 0.230 0.3222222
## 85 18 5.8 1408 9.1 0.130 0.3222222
## 86 18 5.8 1411 3.7 0.260 0.3222222
## 87 18 5.8 1410 5.3 0.270 0.3222222
## 88 18 5.8 1409 7.4 0.260 0.3222222
## 89 18 5.8 1377 17.9 0.110 0.3222222
## 90 18 5.8 1492 19.2 0.120 0.3222222
## 91 18 5.8 1251 23.4 0.038 0.3222222
## 92 18 5.8 1422 30.0 0.044 0.3222222
## 93 18 5.8 1376 38.9 0.046 0.3222222
## 94 19 6.5 <NA> 23.5 0.170 0.3421053
## 95 19 6.5 286 26.0 0.210 0.3421053
## 96 19 6.5 <NA> 0.5 0.320 0.3421053
## 97 19 6.5 5028 0.6 0.520 0.3421053
## 98 19 6.5 942 1.3 0.720 0.3421053
## 99 19 6.5 <NA> 1.4 0.320 0.3421053
## 100 19 6.5 5054 2.6 0.810 0.3421053
## 101 19 6.5 958 3.8 0.640 0.3421053
## 102 19 6.5 952 4.0 0.560 0.3421053
## 103 19 6.5 5165 5.1 0.510 0.3421053
## 104 19 6.5 117 6.2 0.400 0.3421053
## 105 19 6.5 955 6.8 0.610 0.3421053
## 106 19 6.5 5055 7.5 0.260 0.3421053
## 107 19 6.5 <NA> 7.6 0.240 0.3421053
## 108 19 6.5 <NA> 8.4 0.460 0.3421053
## 109 19 6.5 5060 8.5 0.220 0.3421053
## 110 19 6.5 412 8.5 0.230 0.3421053
## 111 19 6.5 5053 10.6 0.280 0.3421053
## 112 19 6.5 5058 12.6 0.380 0.3421053
## 113 19 6.5 5057 12.7 0.270 0.3421053
## 114 19 6.5 <NA> 12.9 0.310 0.3421053
## 115 19 6.5 5051 14.0 0.200 0.3421053
## 116 19 6.5 <NA> 15.0 0.110 0.3421053
## 117 19 6.5 5115 16.0 0.430 0.3421053
## 118 19 6.5 <NA> 17.7 0.270 0.3421053
## 119 19 6.5 931 18.0 0.150 0.3421053
## 120 19 6.5 5056 22.0 0.150 0.3421053
## 121 19 6.5 5059 22.0 0.150 0.3421053
## 122 19 6.5 5061 23.0 0.130 0.3421053
## 123 19 6.5 <NA> 23.2 0.190 0.3421053
## 124 19 6.5 5062 29.0 0.130 0.3421053
## 125 19 6.5 5052 32.0 0.066 0.3421053
## 126 19 6.5 <NA> 32.7 0.350 0.3421053
## 127 19 6.5 724 36.0 0.100 0.3421053
## 128 19 6.5 <NA> 43.5 0.160 0.3421053
## 129 19 6.5 5066 49.0 0.140 0.3421053
## 130 19 6.5 5050 60.0 0.049 0.3421053
## 131 19 6.5 2316 64.0 0.034 0.3421053
## 132 20 5.0 5055 7.5 0.264 0.2500000
## 133 20 5.0 942 8.8 0.263 0.2500000
## 134 20 5.0 5028 8.9 0.230 0.2500000
## 135 20 5.0 5165 9.4 0.147 0.2500000
## 136 20 5.0 952 9.7 0.286 0.2500000
## 137 20 5.0 958 9.7 0.157 0.2500000
## 138 20 5.0 955 10.5 0.237 0.2500000
## 139 20 5.0 117 10.5 0.133 0.2500000
## 140 20 5.0 412 12.0 0.055 0.2500000
## 141 20 5.0 5053 12.2 0.097 0.2500000
## 142 20 5.0 5054 12.8 0.129 0.2500000
## 143 20 5.0 5058 14.6 0.192 0.2500000
## 144 20 5.0 5057 14.9 0.147 0.2500000
## 145 20 5.0 5115 17.6 0.154 0.2500000
## 146 20 5.0 5056 23.9 0.060 0.2500000
## 147 20 5.0 5060 25.0 0.057 0.2500000
## 148 21 5.8 1030 10.8 0.120 0.2761905
## 149 21 5.8 1418 15.7 0.154 0.2761905
## 150 21 5.8 1383 16.7 0.052 0.2761905
## 151 21 5.8 1308 20.8 0.045 0.2761905
## 152 21 5.8 1298 28.5 0.086 0.2761905
## 153 21 5.8 1299 33.1 0.056 0.2761905
## 154 21 5.8 1219 40.3 0.065 0.2761905
## 155 22 5.5 <NA> 4.0 0.259 0.2500000
## 156 22 5.5 <NA> 10.1 0.267 0.2500000
## 157 22 5.5 1030 11.1 0.071 0.2500000
## 158 22 5.5 1418 17.7 0.275 0.2500000
## 159 22 5.5 1383 22.5 0.058 0.2500000
## 160 22 5.5 <NA> 26.5 0.026 0.2500000
## 161 22 5.5 1299 29.0 0.039 0.2500000
## 162 22 5.5 1308 30.9 0.112 0.2500000
## 163 22 5.5 1219 37.8 0.065 0.2500000
## 164 22 5.5 1456 48.3 0.026 0.2500000
## 165 23 5.3 5045 5.8 0.123 0.2304348
## 166 23 5.3 5044 12.0 0.133 0.2304348
## 167 23 5.3 5160 12.1 0.073 0.2304348
## 168 23 5.3 5043 20.5 0.097 0.2304348
## 169 23 5.3 5047 20.5 0.096 0.2304348
## 170 23 5.3 c168 25.3 0.230 0.2304348
## 171 23 5.3 5068 35.9 0.082 0.2304348
## 172 23 5.3 c118 36.1 0.110 0.2304348
## 173 23 5.3 5042 36.3 0.110 0.2304348
## 174 23 5.3 5067 38.5 0.094 0.2304348
## 175 23 5.3 5049 41.4 0.040 0.2304348
## 176 23 5.3 c204 43.6 0.050 0.2304348
## 177 23 5.3 5070 44.4 0.022 0.2304348
## 178 23 5.3 c266 46.1 0.070 0.2304348
## 179 23 5.3 c203 47.1 0.080 0.2304348
## 180 23 5.3 5069 47.7 0.033 0.2304348
## 181 23 5.3 5073 49.2 0.017 0.2304348
## 182 23 5.3 5072 53.1 0.022 0.2304348
Summarise
Perintah summarise berfungsi untuk meringkas atau mengagregasi baris data. Digunakan fungsi group_by agar informasi yang dicetak lebih mudah dianalisis.
attenu %>% group_by(station) %>%
summarise (jumlah_event=sum(event), rata_mag=mean(mag))## # A tibble: 118 x 3
## station jumlah_event rata_mag
## <fct> <dbl> <dbl>
## 1 1008 2 7.4
## 2 1011 4 6.1
## 3 1013 4 6.1
## 4 1014 4 6.1
## 5 1015 4 6.1
## 6 1016 4 6.1
## 7 1027 9 6.6
## 8 1028 30 6
## 9 1030 43 5.65
## 10 1032 14 5.2
## # ... with 108 more rows
Melakukan kelima perintah secara bersamaan
Kita dapat melakukan perintah kelima perintah secara bersamaan untuk mempersingkat program.
attenu %>% select(event, mag, station, accel) %>%
arrange(accel) %>%
filter(mag>7.2) %>%
mutate(earthquakes=mag/event) %>%
summarise (jumlah_event=sum(event), rata_mag=mean(mag))## jumlah_event rata_mag
## 1 104 7.49375