Tugas Praktikum 3.a

Pertanyaan:

Praktikkan penggunaan inner_join(), left_join(), right_join(), full_join(), semi_join(), anti_join() untuk menggabungkan 2 table atau lebih dari database chinook.db atau Northwind_large.sqlite

Jawaban:

untuk menjawab pertanyaan diatas maka akan digunakan tabel albums dan tabel artists dari database chinook. Adapun sintak yang di gunakan yaitu sebagai berikut:

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()
library(RSQLite)
library(DBI)
chinook<-DBI::dbConnect(RSQLite::SQLite(), "C:/sqlite/db/chinook.db")
class(chinook)
## [1] "SQLiteConnection"
## attr(,"package")
## [1] "RSQLite"
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"

Dari output diatas dapat di ketahui bahwa 13 tabel dalam databest chinook. yaitu tabel albums, artists, customers, employees, genres, invoice_items, invoices, media_types, playlist_track, playlists, sqlite_sequence, sqlite_stat1, dan tracks. dari ke-13 tabel tersebut yang akan digunakan dalam tugas ini yaitu tabel albums dan tabel artists. Adapun variabel dari masing masing tabel yaitu sebagai berikut:

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:\sqlite\db\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
artists<-dplyr::tbl(chinook,"artists");
class(artists)
## [1] "tbl_SQLiteConnection" "tbl_dbi"              "tbl_sql"             
## [4] "tbl_lazy"             "tbl"
artists
## # Source:   table<artists> [?? x 2]
## # Database: sqlite 3.36.0 [C:\sqlite\db\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

Dari output diatas diketahui bahwa terdapat kolom yang sama yang terdapat pada tabel albums dan tabel artists yaitu kolom artisid. sehingga kedua tabel tersebut dapat dilakukan operasional penggabungan tabel dengan beberapa cara sebagai berikut:

inner_join()

inner_joint() digunakan untuk menghasilkan semua baris pada table albums yang memiliki kesamaan nilai dengan table artist, dan semua kolom dari tabel albums dan tabel artist. Adapun hasilnya sebagai berikut:

inner_join(albums, artists)
## Joining, by = "ArtistId"
## # Source:   lazy query [?? x 4]
## # Database: sqlite 3.36.0 [C:\sqlite\db\chinook.db]
##    AlbumId Title                                 ArtistId Name                
##      <int> <chr>                                    <int> <chr>               
##  1       1 For Those About To Rock We Salute You        1 AC/DC               
##  2       2 Balls to the Wall                            2 Accept              
##  3       3 Restless and Wild                            2 Accept              
##  4       4 Let There Be Rock                            1 AC/DC               
##  5       5 Big Ones                                     3 Aerosmith           
##  6       6 Jagged Little Pill                           4 Alanis Morissette   
##  7       7 Facelift                                     5 Alice In Chains     
##  8       8 Warner 25 Anos                               6 Antônio Carlos Jobim
##  9       9 Plays Metallica By Four Cellos               7 Apocalyptica        
## 10      10 Audioslave                                   8 Audioslave          
## # ... with more rows

left_join()

left_join() digunakan untuk menghasilkan semua baris pada table albums, dan semua kolom pada tabel albums dan tabel artists, dimana untuk baris pada albums yang tidak memiliki kesamaan dengan artist diisi dengan nilai NA pada kolom yang baru. adapun hasil yang diperoleh adalah sebagai berikut:

left_join(albums, artists)
## Joining, by = "ArtistId"
## # Source:   lazy query [?? x 4]
## # Database: sqlite 3.36.0 [C:\sqlite\db\chinook.db]
##    AlbumId Title                                 ArtistId Name                
##      <int> <chr>                                    <int> <chr>               
##  1       1 For Those About To Rock We Salute You        1 AC/DC               
##  2       2 Balls to the Wall                            2 Accept              
##  3       3 Restless and Wild                            2 Accept              
##  4       4 Let There Be Rock                            1 AC/DC               
##  5       5 Big Ones                                     3 Aerosmith           
##  6       6 Jagged Little Pill                           4 Alanis Morissette   
##  7       7 Facelift                                     5 Alice In Chains     
##  8       8 Warner 25 Anos                               6 Antônio Carlos Jobim
##  9       9 Plays Metallica By Four Cellos               7 Apocalyptica        
## 10      10 Audioslave                                   8 Audioslave          
## # ... with more rows

right_join()

right_join() digunakan untuk menghasilkan semua baris pada table artists, dan semua kolom pada albums dan artists, dimana untuk baris pada tabel artist yang tidak memiliki kesamaan dengan tabel albums akan diisi dengan nilai NA pada kolom yang baru. adapun hasil yang di peroleh adalah sebagai berikut:

right_join(albums, artists)
## Joining, by = "ArtistId"
## # Source:   lazy query [?? x 4]
## # Database: sqlite 3.36.0 [C:\sqlite\db\chinook.db]
##    AlbumId Title                                 ArtistId Name                
##      <int> <chr>                                    <int> <chr>               
##  1       1 For Those About To Rock We Salute You        1 AC/DC               
##  2       4 Let There Be Rock                            1 AC/DC               
##  3       2 Balls to the Wall                            2 Accept              
##  4       3 Restless and Wild                            2 Accept              
##  5       5 Big Ones                                     3 Aerosmith           
##  6       6 Jagged Little Pill                           4 Alanis Morissette   
##  7       7 Facelift                                     5 Alice In Chains     
##  8       8 Warner 25 Anos                               6 Antônio Carlos Jobim
##  9      34 Chill: Brazil (Disc 2)                       6 Antônio Carlos Jobim
## 10       9 Plays Metallica By Four Cellos               7 Apocalyptica        
## # ... with more rows

full_join()

full_join() digunakan untuk menghasilkan semua baris dan kolom dari albums dan artists. Jika ada nilai yang tidak sama (match) antara albums dan artists, maka akan bernilai NA. Adapun hasil yang diperoleh adalah sebagai berikut:

full_join(albums, artists)
## Joining, by = "ArtistId"
## # Source:   lazy query [?? x 4]
## # Database: sqlite 3.36.0 [C:\sqlite\db\chinook.db]
##    AlbumId Title ArtistId Name                      
##      <int> <chr>    <int> <chr>                     
##  1      NA <NA>        25 Milton Nascimento & Bebeto
##  2      NA <NA>        26 Azymuth                   
##  3      NA <NA>        28 João Gilberto             
##  4      NA <NA>        29 Bebel Gilberto            
##  5      NA <NA>        30 Jorge Vercilo             
##  6      NA <NA>        31 Baby Consuelo             
##  7      NA <NA>        32 Ney Matogrosso            
##  8      NA <NA>        33 Luiz Melodia              
##  9      NA <NA>        34 Nando Reis                
## 10      NA <NA>        35 Pedro Luís & A Parede     
## # ... with more rows

semi_join()

semi_join() digunakan untuk menghasilkan semua baris pada table albums yang memiliki kesamaan nilai dengan tabel artist, dan semua kolom dari albums. Adapun hasil yang diperoleh adalah sebagai berikut:

semi_join(albums, artists)
## Joining, by = "ArtistId"
## # Source:   lazy query [?? x 3]
## # Database: sqlite 3.36.0 [C:\sqlite\db\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()

anti_join() digunakan untuk menghasilkan semua baris dari albums yang TIDAK memiliki kesamaan dengan tabel artists, dan semua kolom yang berasal dari Albums. Adapun hasil yang di peroleh adalah sebagai berikut:

anti_join(albums, artists)
## Joining, by = "ArtistId"
## # Source:   lazy query [?? x 3]
## # Database: sqlite 3.36.0 [C:\sqlite\db\chinook.db]
## # ... with 3 variables: AlbumId <int>, Title <chr>, ArtistId <int>

Dari hasil output anti_join() diatas tidak terdapat album yang tidak memiliki kesamaan dengan tabel artist.

Tugas Praktikum 3.b

Pertanyaan:

Buat satu Peta dari daerah Anda masing-masing (Kecamatan, Kabupaten), Anda bebas menggunakan data apapun.

Jawaban:

untuk menjawab pertanyaan diatas maka akan digunakan latittude dan longtitude tempat tinggal. Adapun sintak yang di gunakan yaitu sebagai berikut:

library(leaflet)
map <- leaflet() %>% setView(lng =      107.4712736651738, lat =    -7.108484364416049 , zoom = 12)
map %>% addTiles()