Ada beberapa jenis menyatukan 2 tabel pada R, seperti “inner join”, “left join”, “right join”, “full join”, “semi join”, dan “anti join”. Dalam penggunaan fungsi “join” serta untuk memanggil data diperlukan beberapa package, sehingga perlu install RODBC, RSQLite, DBI, dan dipanggil dahulu package tersebut.
library(DBI)
library(RODBC)
library(RSQLite)
library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.5 v purrr 0.3.4
## v tibble 3.1.4 v dplyr 1.0.7
## v tidyr 1.1.3 v stringr 1.4.0
## v readr 2.0.1 v forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
Kemudian import Database. Data yang akan digunakan adalah data chinook, data ini menampilkan suatu toko media digital yang didalamnya ada tabel albums, artists, customers, employees, genres, dan lain-lain.
chinook <- dbConnect(RSQLite::SQLite(), "C:/Users/Asus/chinook.db")
chinook
## <SQLiteConnection>
## Path: C:\Users\Asus\chinook.db
## Extensions: TRUE
class(chinook)
## [1] "SQLiteConnection"
## attr(,"package")
## [1] "RSQLite"
Berikut adalah fungsi untuk melihat tabel apa saja yang ada pada Database.
RSQLite::dbListTables(chinook)
## [1] "albums" "artists" "customers" "employees"
## [5] "genres" "invoice_items" "invoices" "media_types"
## [9] "playlist_track" "playlists" "sqlite_sequence" "sqlite_stat1"
## [13] "tracks"
Tabel yang akan digunakan untuk pengaplikasian fungsi “join” adalah tabel artists dan albums.
artists<-dplyr::tbl(chinook,"artists")
class(artists)
## [1] "tbl_SQLiteConnection" "tbl_dbi" "tbl_sql"
## [4] "tbl_lazy" "tbl"
Tabel artists ini berisi kolom “ArtistId” dan “Name”, sehingga setiap artis memiliki Id masing-masing. Artis yang terdaftar pada tabel ini sebanyak 275 artis.
artists
## # Source: table<artists> [?? x 2]
## # Database: sqlite 3.36.0 [C:\Users\Asus\chinook.db]
## ArtistId Name
## <int> <chr>
## 1 1 AC/DC
## 2 2 Accept
## 3 3 Aerosmith
## 4 4 Alanis Morissette
## 5 5 Alice In Chains
## 6 6 Antônio Carlos Jobim
## 7 7 Apocalyptica
## 8 8 Audioslave
## 9 9 BackBeat
## 10 10 Billy Cobham
## # ... with more rows
Tabel albums terdiri dari 3 kolom yaitu AlbumId, Title, dan ArtisId. Setiap album mempunyai Id masing-masing, jumlah Id Album yaitu 347. Setiap artis memungkinkan untuk mempunyai album lebih dari 1, sehingga pada kolom ArtisId bisa berisi angka yang sama. Selain itu, tidak semua artis memiliki album.
albums<-dplyr::tbl(chinook,"albums")
class(albums)
## [1] "tbl_SQLiteConnection" "tbl_dbi" "tbl_sql"
## [4] "tbl_lazy" "tbl"
albums
## # Source: table<albums> [?? x 3]
## # Database: sqlite 3.36.0 [C:\Users\Asus\chinook.db]
## AlbumId Title ArtistId
## <int> <chr> <int>
## 1 1 For Those About To Rock We Salute You 1
## 2 2 Balls to the Wall 2
## 3 3 Restless and Wild 2
## 4 4 Let There Be Rock 1
## 5 5 Big Ones 3
## 6 6 Jagged Little Pill 4
## 7 7 Facelift 5
## 8 8 Warner 25 Anos 6
## 9 9 Plays Metallica By Four Cellos 7
## 10 10 Audioslave 8
## # ... with more rows
inner_join() menghasilkan semua kolom tabel artists dan albums. Kemudian, antara kedua tabel tersebut terdapat kolom yang sama yaitu ArtistId sehingga akan ditampilkan semua baris pada tabel artists yang memiliki kesamaan nilai dengan tabel albums.
inner_join(artists,albums)
## Joining, by = "ArtistId"
## # Source: lazy query [?? x 4]
## # Database: sqlite 3.36.0 [C:\Users\Asus\chinook.db]
## ArtistId Name AlbumId Title
## <int> <chr> <int> <chr>
## 1 1 AC/DC 1 For Those About To Rock We Salute You
## 2 2 Accept 2 Balls to the Wall
## 3 2 Accept 3 Restless and Wild
## 4 1 AC/DC 4 Let There Be Rock
## 5 3 Aerosmith 5 Big Ones
## 6 4 Alanis Morissette 6 Jagged Little Pill
## 7 5 Alice In Chains 7 Facelift
## 8 6 Antônio Carlos Jobim 8 Warner 25 Anos
## 9 7 Apocalyptica 9 Plays Metallica By Four Cellos
## 10 8 Audioslave 10 Audioslave
## # ... with more rows
artists %>% inner_join(albums)
## Joining, by = "ArtistId"
## # Source: lazy query [?? x 4]
## # Database: sqlite 3.36.0 [C:\Users\Asus\chinook.db]
## ArtistId Name AlbumId Title
## <int> <chr> <int> <chr>
## 1 1 AC/DC 1 For Those About To Rock We Salute You
## 2 2 Accept 2 Balls to the Wall
## 3 2 Accept 3 Restless and Wild
## 4 1 AC/DC 4 Let There Be Rock
## 5 3 Aerosmith 5 Big Ones
## 6 4 Alanis Morissette 6 Jagged Little Pill
## 7 5 Alice In Chains 7 Facelift
## 8 6 Antônio Carlos Jobim 8 Warner 25 Anos
## 9 7 Apocalyptica 9 Plays Metallica By Four Cellos
## 10 8 Audioslave 10 Audioslave
## # ... with more rows
Karena setiap artis memungkinkan membuat lebih dari 1 album, sehingga akan ada data yang berulang pada kolom ArtistId dan Name.
left_join() menghasilkan semua baris pada tabel artists dan semua kolom pada tabel artists dan albums. Untuk baris pada tabel artists yang tidak memiliki kesamaan dengan y diisi dengan nilai NA pada kolom yang baru, yaitu di mana artis yang tidak membuat album.
left_join(artists,albums)
## Joining, by = "ArtistId"
## # Source: lazy query [?? x 4]
## # Database: sqlite 3.36.0 [C:\Users\Asus\chinook.db]
## ArtistId Name AlbumId Title
## <int> <chr> <int> <chr>
## 1 1 AC/DC 1 For Those About To Rock We Salute You
## 2 1 AC/DC 4 Let There Be Rock
## 3 2 Accept 2 Balls to the Wall
## 4 2 Accept 3 Restless and Wild
## 5 3 Aerosmith 5 Big Ones
## 6 4 Alanis Morissette 6 Jagged Little Pill
## 7 5 Alice In Chains 7 Facelift
## 8 6 Antônio Carlos Jobim 8 Warner 25 Anos
## 9 6 Antônio Carlos Jobim 34 Chill: Brazil (Disc 2)
## 10 7 Apocalyptica 9 Plays Metallica By Four Cellos
## # ... with more rows
right_join() menghasilkan semua baris pada tabel albums dan semua kolom pada tabel artists dan albums. Untuk baris pada tabel albums yang tidak memiliki kesamaan dengan y diisi dengan nilai NA pada kolom yang baru. Karena seluruh artist yang membuat album pasti terdaftar pada tabel artists, sehingga tidak ada data yang diisi NA. Sehingga yang dilakukan disini hanya menambahkan kolom “Name” dari tabel artist, yang sebelumnya tidak ada di tabel albums (hanya ArtistIdnya).
right_join(artists,albums)
## Joining, by = "ArtistId"
## # Source: lazy query [?? x 4]
## # Database: sqlite 3.36.0 [C:\Users\Asus\chinook.db]
## ArtistId Name AlbumId Title
## <int> <chr> <int> <chr>
## 1 1 AC/DC 1 For Those About To Rock We Salute You
## 2 2 Accept 2 Balls to the Wall
## 3 2 Accept 3 Restless and Wild
## 4 1 AC/DC 4 Let There Be Rock
## 5 3 Aerosmith 5 Big Ones
## 6 4 Alanis Morissette 6 Jagged Little Pill
## 7 5 Alice In Chains 7 Facelift
## 8 6 Antônio Carlos Jobim 8 Warner 25 Anos
## 9 7 Apocalyptica 9 Plays Metallica By Four Cellos
## 10 8 Audioslave 10 Audioslave
## # ... with more rows
full_join() menghasilkan semua baris dan kolom pada tabel artist dan albums, sehingga ketika ada nilai yang tidak sama antara kedua tabel, maka akan bernilai NA. Seperti yang sudah diketahui, kedua tabel memiliki kesamaan pada ArtistId. Kemudian seluruh ArtistId yang ada pada tabel albums, ada juga pada tabel artists. Sehingga interpretasi dari full_join(artists,albums) sama dengan left_join(artists,albums).
full_join(artists,albums)
## Joining, by = "ArtistId"
## # Source: lazy query [?? x 4]
## # Database: sqlite 3.36.0 [C:\Users\Asus\chinook.db]
## ArtistId Name AlbumId Title
## <int> <chr> <int> <chr>
## 1 1 AC/DC 1 For Those About To Rock We Salute You
## 2 1 AC/DC 4 Let There Be Rock
## 3 2 Accept 2 Balls to the Wall
## 4 2 Accept 3 Restless and Wild
## 5 3 Aerosmith 5 Big Ones
## 6 4 Alanis Morissette 6 Jagged Little Pill
## 7 5 Alice In Chains 7 Facelift
## 8 6 Antônio Carlos Jobim 8 Warner 25 Anos
## 9 6 Antônio Carlos Jobim 34 Chill: Brazil (Disc 2)
## 10 7 Apocalyptica 9 Plays Metallica By Four Cellos
## # ... with more rows
semi_join(artists,albums) menghasilkan semua baris pada tabel artists yang memiliki kesamaan nilai dengan tabel albums, dan menghasilkan semua kolom dari tabel artists. Sehingga fungsi ini akan memberikan output nama artis yang memiliki album.
semi_join(artists,albums)
## Joining, by = "ArtistId"
## # Source: lazy query [?? x 2]
## # Database: sqlite 3.36.0 [C:\Users\Asus\chinook.db]
## ArtistId Name
## <int> <chr>
## 1 1 AC/DC
## 2 2 Accept
## 3 3 Aerosmith
## 4 4 Alanis Morissette
## 5 5 Alice In Chains
## 6 6 Antônio Carlos Jobim
## 7 7 Apocalyptica
## 8 8 Audioslave
## 9 9 BackBeat
## 10 10 Billy Cobham
## # ... with more rows
semi_join(albums,artists) menghasilkan semua baris pada tabel albums yang memiliki kesamaan nilai dengan tabel artists, dan menghasilkan semua kolom dari tabel albums. Dikarenakan semua ArtistId pada tabel albums, ada juga pada tabel artists. Sehingga output fungsi ini hanya menampilkan tabel albums.
semi_join(albums,artists)
## Joining, by = "ArtistId"
## # Source: lazy query [?? x 3]
## # Database: sqlite 3.36.0 [C:\Users\Asus\chinook.db]
## AlbumId Title ArtistId
## <int> <chr> <int>
## 1 1 For Those About To Rock We Salute You 1
## 2 2 Balls to the Wall 2
## 3 3 Restless and Wild 2
## 4 4 Let There Be Rock 1
## 5 5 Big Ones 3
## 6 6 Jagged Little Pill 4
## 7 7 Facelift 5
## 8 8 Warner 25 Anos 6
## 9 9 Plays Metallica By Four Cellos 7
## 10 10 Audioslave 8
## # ... with more rows
anti_join(artists,albums) menghasilkan semua baris dari tabel artists yang tidak memiliki kesamaan dengan tabel albums, serta menampilkan kolom dari tabel artists. Sehingga fungsi ini menampilkan nama artis beserta Idnya yang tidak mempunyai album.
anti_join(artists,albums)
## Joining, by = "ArtistId"
## # Source: lazy query [?? x 2]
## # Database: sqlite 3.36.0 [C:\Users\Asus\chinook.db]
## ArtistId Name
## <int> <chr>
## 1 25 Milton Nascimento & Bebeto
## 2 26 Azymuth
## 3 28 João Gilberto
## 4 29 Bebel Gilberto
## 5 30 Jorge Vercilo
## 6 31 Baby Consuelo
## 7 32 Ney Matogrosso
## 8 33 Luiz Melodia
## 9 34 Nando Reis
## 10 35 Pedro Luís & A Parede
## # ... with more rows
anti_join(albums,artists) menghasilkan semua baris dari tabel albums yang tidak memiliki kesamaan dengan tabel artists, serta menampilkan kolom dari tabel albums. Dikarenakan penyatuan tabel ini berdasarkan “ArtistId”, kemudian semua nilai ArtistId pada tabel albums, ada juga pada tabel artists maka fungsi ini tidak mengeluarkan output apapun.
anti_join(albums,artists)
## Joining, by = "ArtistId"
## # Source: lazy query [?? x 3]
## # Database: sqlite 3.36.0 [C:\Users\Asus\chinook.db]
## # ... with 3 variables: AlbumId <int>, Title <chr>, ArtistId <int>