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.
data1 <- DBI::dbConnect(RSQLite::SQLite(), "C:/sqlite/Chinook.db")Syntax menunjukkan pembuatan variabel data1 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
MediaTypeId==2;| TrackId | Name | AlbumId | MediaTypeId | GenreId | Composer | Milliseconds | Bytes | UnitPrice |
|---|---|---|---|---|---|---|---|---|
| 2 | Balls to the Wall | 2 | 2 | 1 | NA | 342562 | 5510424 | 0.99 |
| 3 | Fast As a Shark | 3 | 2 | 1 | F. Baltes, S. Kaufman, U. Dirkscneider & W. Hoffman | 230619 | 3990994 | 0.99 |
| 4 | Restless and Wild | 3 | 2 | 1 | F. Baltes, R.A. Smith-Diesel, S. Kaufman, U. Dirkscneider & W. Hoffman | 252051 | 4331779 | 0.99 |
| 5 | Princess of the Dawn | 3 | 2 | 1 | Deaffy & R.A. Smith-Diesel | 375418 | 6290521 | 0.99 |
| 1146 | Welcome to the Jungle | 90 | 2 | 1 | NA | 273552 | 4538451 | 0.99 |
| 1147 | It’s So Easy | 90 | 2 | 1 | NA | 202824 | 3394019 | 0.99 |
| 1148 | Nightrain | 90 | 2 | 1 | NA | 268537 | 4457283 | 0.99 |
| 1149 | Out Ta Get Me | 90 | 2 | 1 | NA | 263893 | 4382147 | 0.99 |
| 1150 | Mr. Brownstone | 90 | 2 | 1 | NA | 228924 | 3816323 | 0.99 |
| 1151 | Paradise City | 90 | 2 | 1 | NA | 406347 | 6687123 | 0.99 |
2. Menggunakan DPLYR
Diperlukan variabel baru untuk memanggil suatu database.
data2 <- DBI::dbConnect(RSQLite::SQLite(), "C:/sqlite/Chinook.db")dengan kelas:
class(data2)## [1] "SQLiteConnection"
## attr(,"package")
## [1] "RSQLite"
Untuk melihat ada tabel apa saja pada chinook:
RSQLite::dbListTables(data2)## [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(data2, "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 :
data3 <- tracks %>%
select(TrackId, GenreId, Milliseconds, Bytes) %>%
filter(Bytes>4000000) %>%
arrange(TrackId)
data3## # Source: lazy query [?? x 4]
## # Database: sqlite 3.37.0 [C:\sqlite\chinook.db]
## # Ordered by: TrackId
## TrackId GenreId Milliseconds Bytes
## <int> <int> <int> <int>
## 1 1 1 343719 11170334
## 2 2 1 342562 5510424
## 3 4 1 252051 4331779
## 4 5 1 375418 6290521
## 5 6 1 205662 6713451
## 6 7 1 233926 7636561
## 7 8 1 210834 6852860
## 8 9 1 203102 6599424
## 9 10 1 263497 8611245
## 10 11 1 199836 6566314
## # ... with more rows
Syntax di atas memanggil tabel Tracks dan mencetak TrackId, GenreId, Milliseconds, dan Bytes dengan byte lebih dari 4000000 lalu diurutkan berdasarkan TrackId.
Pengguna dapat menggunakan syntax berikut untuk melihat bagaimana jika fungsi tersebut dijalankan di SQL:
dplyr::show_query(data3)## <SQL>
## SELECT *
## FROM (SELECT `TrackId`, `GenreId`, `Milliseconds`, `Bytes`
## FROM `Tracks`)
## WHERE (`Bytes` > 4000000.0)
## ORDER BY `TrackId`
B. Data Wrangling
Pada tahap ini kita akan menggunakan dataset mtcars yang telah tersedia pada default datasets di R.
library(datasets)
mtcarsSelect
Perintah select berfungsi untuk memilih kolom tertentu saja untuk dicetak.
mtcars %>% select(mpg, cyl, am, gear)## mpg cyl am gear
## Mazda RX4 21.0 6 1 4
## Mazda RX4 Wag 21.0 6 1 4
## Datsun 710 22.8 4 1 4
## Hornet 4 Drive 21.4 6 0 3
## Hornet Sportabout 18.7 8 0 3
## Valiant 18.1 6 0 3
## Duster 360 14.3 8 0 3
## Merc 240D 24.4 4 0 4
## Merc 230 22.8 4 0 4
## Merc 280 19.2 6 0 4
## Merc 280C 17.8 6 0 4
## Merc 450SE 16.4 8 0 3
## Merc 450SL 17.3 8 0 3
## Merc 450SLC 15.2 8 0 3
## Cadillac Fleetwood 10.4 8 0 3
## Lincoln Continental 10.4 8 0 3
## Chrysler Imperial 14.7 8 0 3
## Fiat 128 32.4 4 1 4
## Honda Civic 30.4 4 1 4
## Toyota Corolla 33.9 4 1 4
## Toyota Corona 21.5 4 0 3
## Dodge Challenger 15.5 8 0 3
## AMC Javelin 15.2 8 0 3
## Camaro Z28 13.3 8 0 3
## Pontiac Firebird 19.2 8 0 3
## Fiat X1-9 27.3 4 1 4
## Porsche 914-2 26.0 4 1 5
## Lotus Europa 30.4 4 1 5
## Ford Pantera L 15.8 8 1 5
## Ferrari Dino 19.7 6 1 5
## Maserati Bora 15.0 8 1 5
## Volvo 142E 21.4 4 1 4
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.
mtcars %>% arrange(cyl)## mpg cyl disp hp drat wt qsec vs am gear carb
## Datsun 710 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1
## Merc 240D 24.4 4 146.7 62 3.69 3.190 20.00 1 0 4 2
## Merc 230 22.8 4 140.8 95 3.92 3.150 22.90 1 0 4 2
## 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
## Toyota Corona 21.5 4 120.1 97 3.70 2.465 20.01 1 0 3 1
## Fiat X1-9 27.3 4 79.0 66 4.08 1.935 18.90 1 1 4 1
## Porsche 914-2 26.0 4 120.3 91 4.43 2.140 16.70 0 1 5 2
## Lotus Europa 30.4 4 95.1 113 3.77 1.513 16.90 1 1 5 2
## Volvo 142E 21.4 4 121.0 109 4.11 2.780 18.60 1 1 4 2
## 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
## Hornet 4 Drive 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1
## Valiant 18.1 6 225.0 105 2.76 3.460 20.22 1 0 3 1
## Merc 280 19.2 6 167.6 123 3.92 3.440 18.30 1 0 4 4
## Merc 280C 17.8 6 167.6 123 3.92 3.440 18.90 1 0 4 4
## Ferrari Dino 19.7 6 145.0 175 3.62 2.770 15.50 0 1 5 6
## Hornet Sportabout 18.7 8 360.0 175 3.15 3.440 17.02 0 0 3 2
## Duster 360 14.3 8 360.0 245 3.21 3.570 15.84 0 0 3 4
## Merc 450SE 16.4 8 275.8 180 3.07 4.070 17.40 0 0 3 3
## Merc 450SL 17.3 8 275.8 180 3.07 3.730 17.60 0 0 3 3
## Merc 450SLC 15.2 8 275.8 180 3.07 3.780 18.00 0 0 3 3
## Cadillac Fleetwood 10.4 8 472.0 205 2.93 5.250 17.98 0 0 3 4
## Lincoln Continental 10.4 8 460.0 215 3.00 5.424 17.82 0 0 3 4
## Chrysler Imperial 14.7 8 440.0 230 3.23 5.345 17.42 0 0 3 4
## Dodge Challenger 15.5 8 318.0 150 2.76 3.520 16.87 0 0 3 2
## AMC Javelin 15.2 8 304.0 150 3.15 3.435 17.30 0 0 3 2
## Camaro Z28 13.3 8 350.0 245 3.73 3.840 15.41 0 0 3 4
## Pontiac Firebird 19.2 8 400.0 175 3.08 3.845 17.05 0 0 3 2
## Ford Pantera L 15.8 8 351.0 264 4.22 3.170 14.50 0 1 5 4
## Maserati Bora 15.0 8 301.0 335 3.54 3.570 14.60 0 1 5 8
Filter
Perintah filter berfungsi untuk menyeleksi atau memilih baris berdasarkan ketentuan tertentu.
mtcars %>% filter(gear>3)## 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
## Merc 240D 24.4 4 146.7 62 3.69 3.190 20.00 1 0 4 2
## 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
## Merc 280C 17.8 6 167.6 123 3.92 3.440 18.90 1 0 4 4
## 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
## Fiat X1-9 27.3 4 79.0 66 4.08 1.935 18.90 1 1 4 1
## Porsche 914-2 26.0 4 120.3 91 4.43 2.140 16.70 0 1 5 2
## Lotus Europa 30.4 4 95.1 113 3.77 1.513 16.90 1 1 5 2
## Ford Pantera L 15.8 8 351.0 264 4.22 3.170 14.50 0 1 5 4
## Ferrari Dino 19.7 6 145.0 175 3.62 2.770 15.50 0 1 5 6
## Maserati Bora 15.0 8 301.0 335 3.54 3.570 14.60 0 1 5 8
## Volvo 142E 21.4 4 121.0 109 4.11 2.780 18.60 1 1 4 2
Mutate
mutate adalah perintah yang dapat membuat kolom baru dari kolom yang sudah ada.
mtcars %>% mutate(double=2*cyl)## mpg cyl disp hp drat wt qsec vs am gear carb double
## Mazda RX4 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4 12
## Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4 12
## Datsun 710 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1 8
## Hornet 4 Drive 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1 12
## Hornet Sportabout 18.7 8 360.0 175 3.15 3.440 17.02 0 0 3 2 16
## Valiant 18.1 6 225.0 105 2.76 3.460 20.22 1 0 3 1 12
## Duster 360 14.3 8 360.0 245 3.21 3.570 15.84 0 0 3 4 16
## Merc 240D 24.4 4 146.7 62 3.69 3.190 20.00 1 0 4 2 8
## Merc 230 22.8 4 140.8 95 3.92 3.150 22.90 1 0 4 2 8
## Merc 280 19.2 6 167.6 123 3.92 3.440 18.30 1 0 4 4 12
## Merc 280C 17.8 6 167.6 123 3.92 3.440 18.90 1 0 4 4 12
## Merc 450SE 16.4 8 275.8 180 3.07 4.070 17.40 0 0 3 3 16
## Merc 450SL 17.3 8 275.8 180 3.07 3.730 17.60 0 0 3 3 16
## Merc 450SLC 15.2 8 275.8 180 3.07 3.780 18.00 0 0 3 3 16
## Cadillac Fleetwood 10.4 8 472.0 205 2.93 5.250 17.98 0 0 3 4 16
## Lincoln Continental 10.4 8 460.0 215 3.00 5.424 17.82 0 0 3 4 16
## Chrysler Imperial 14.7 8 440.0 230 3.23 5.345 17.42 0 0 3 4 16
## Fiat 128 32.4 4 78.7 66 4.08 2.200 19.47 1 1 4 1 8
## Honda Civic 30.4 4 75.7 52 4.93 1.615 18.52 1 1 4 2 8
## Toyota Corolla 33.9 4 71.1 65 4.22 1.835 19.90 1 1 4 1 8
## Toyota Corona 21.5 4 120.1 97 3.70 2.465 20.01 1 0 3 1 8
## Dodge Challenger 15.5 8 318.0 150 2.76 3.520 16.87 0 0 3 2 16
## AMC Javelin 15.2 8 304.0 150 3.15 3.435 17.30 0 0 3 2 16
## Camaro Z28 13.3 8 350.0 245 3.73 3.840 15.41 0 0 3 4 16
## Pontiac Firebird 19.2 8 400.0 175 3.08 3.845 17.05 0 0 3 2 16
## Fiat X1-9 27.3 4 79.0 66 4.08 1.935 18.90 1 1 4 1 8
## Porsche 914-2 26.0 4 120.3 91 4.43 2.140 16.70 0 1 5 2 8
## Lotus Europa 30.4 4 95.1 113 3.77 1.513 16.90 1 1 5 2 8
## Ford Pantera L 15.8 8 351.0 264 4.22 3.170 14.50 0 1 5 4 16
## Ferrari Dino 19.7 6 145.0 175 3.62 2.770 15.50 0 1 5 6 12
## Maserati Bora 15.0 8 301.0 335 3.54 3.570 14.60 0 1 5 8 16
## Volvo 142E 21.4 4 121.0 109 4.11 2.780 18.60 1 1 4 2 8
Summarise
Perintah summarise berfungsi untuk meringkas atau mengagregasi baris data. Digunakan fungsi group_by agar informasi yang dicetak lebih mudah dianalisis.
mtcars %>% group_by(am) %>%
summarise(sum_cyl=sum(cyl), avg_gear=mean(gear))## # A tibble: 2 x 3
## am sum_cyl avg_gear
## <dbl> <dbl> <dbl>
## 1 0 132 3.21
## 2 1 66 4.38
Melakukan kelima perintah secara bersamaan
Kita dapat melakukan perintah kelima perintah secara bersamaan untuk mempersingkat program.
mtcars %>% select(mpg, cyl, am, gear) %>%
arrange(cyl) %>%
filter(gear>3) %>%
mutate(double=2*cyl) %>%
summarise (sum_cyl=sum(cyl), avg_gear=mean(gear))## sum_cyl avg_gear
## 1 86 4.294118