A. Akses dan Wrangling Database di R dengan Engine SQL ~

  1. Akses database Company.db menggunakan fungsi dbconnect() dan SQLite() dari library DBI dan RSQLite
  2. db <- DBI::dbConnect(RSQLite::SQLite(),"D:/Users/Documents/MANDAREL/Company.db")

  3. Menampilkan data EMPLOYEE
  4. SELECT 
    * 
    FROM 
    EMPLOYEE;
    8 records
    Fname Minit Lname Ssn Bdate Address Sex Salary Super_ssn Dno
    John B Smith 123456789 1965-01-09 731 Fondren, Houston TX M 30000 333445555 5
    Franklin T Wong 333445555 1965-12-08 638 Voss, Houston TX M 40000 888665555 5
    Alicia J Zelaya 999887777 1968-01-19 3321 Castle, Spring TX F 25000 987654321 4
    Jennifer S Wallace 987654321 1941-06-20 291 Berry, Bellaire TX F 43000 888665555 4
    Ramesh K Narayan 666884444 1962-09-15 975 Fire Oak, Humble TX M 38000 333445555 5
    Joyce A English 453453453 1972-07-31 5631 Rice, Houston TX F 25000 333445555 5
    Ahmad V Jabbar 987987987 1969-03-29 980 Dallas, Houston TX M 25000 987654321 4
    James E Borg 888665555 1937-11-10 450 Stone, Houston TX M 55000 NA 1

  5. Melakukan wrangling database dengan ketentuan berikut
  6. /*Pilih variabel Fname, Minit, Lname yang digabungkan menjadi nama lengkap, kemudian Ssn, Sex, Salary, dan Dno, masing-masing disimpan dengan nama tertentu, lalu konversi variabel Bdate (tanggal lahir) pada substring yang menunjukkan tahun kelahiran bagi pegawai dengan rentang gaji lebih besar dari 30000 serta telah terurut berdasarkan tahun kelahiran*/
    SELECT
      Fname ||' '|| Minit||' '||Lname as Nama_Lengkap, Ssn as Nomor_Induk_Pegawai, Sex as Jenis_Kelamin, substring(Bdate,1,4) as Tahun_Lahir, Salary as Gaji, Dno as Nomor_Departemen
    FROM 
      EMPLOYEE
    WHERE
      Gaji>30000
    ORDER BY
      Tahun_Lahir;
    4 records
    Nama_Lengkap Nomor_Induk_Pegawai Jenis_Kelamin Tahun_Lahir Gaji Nomor_Departemen
    James E Borg 888665555 M 1937 55000 1
    Jennifer S Wallace 987654321 F 1941 43000 4
    Ramesh K Narayan 666884444 M 1962 38000 5
    Franklin T Wong 333445555 M 1965 40000 5

B. Akses dan Wrangling Database di R dengan dplyr ~

  1. Mengakses database Company.db dengan dbConnect() dan disimpan dalam objek baru
  2. Data.Perusahaan <- DBI::dbConnect(RSQLite::SQLite(),
                         "D:/Users/Documents/MANDAREL/Company.db")

  3. Ingin mengetahui berapa dan apa saja entity yang ada pada database tersebut
  4. RSQLite::dbListTables(Data.Perusahaan)
    ## [1] "DEPARTMENT"     "DEPENDENT"      "DEPT_LOCATIONS" "EMPLOYEE"      
    ## [5] "PROJECT"        "WORKS_ON"

  5. Mengaktifkan paket dplyr
  6. library(dplyr)
    ## Warning: package 'dplyr' was built under R version 4.0.5
    ## 
    ## Attaching package: 'dplyr'
    ## The following objects are masked from 'package:stats':
    ## 
    ##     filter, lag
    ## The following objects are masked from 'package:base':
    ## 
    ##     intersect, setdiff, setequal, union

  7. Mengambil entitity EMPLOYEE pada Data.Perusahaan dalam format tibble dan disimpan dalam objek baru
  8. Employee <- tbl(Data.Perusahaan, "EMPLOYEE"); Employee
    ## # Source:   table<EMPLOYEE> [?? x 10]
    ## # Database: sqlite 3.37.0 [D:\Users\Documents\MANDAREL\Company.db]
    ##   Fname    Minit Lname   Ssn       Bdate  Address   Sex   Salary Super_ssn   Dno
    ##   <chr>    <chr> <chr>   <chr>     <chr>  <chr>     <chr>  <int> <chr>     <int>
    ## 1 John     B     Smith   123456789 1965-~ 731 Fond~ M      30000 333445555     5
    ## 2 Franklin T     Wong    333445555 1965-~ 638 Voss~ M      40000 888665555     5
    ## 3 Alicia   J     Zelaya  999887777 1968-~ 3321 Cas~ F      25000 987654321     4
    ## 4 Jennifer S     Wallace 987654321 1941-~ 291 Berr~ F      43000 888665555     4
    ## 5 Ramesh   K     Narayan 666884444 1962-~ 975 Fire~ M      38000 333445555     5
    ## 6 Joyce    A     English 453453453 1972-~ 5631 Ric~ F      25000 333445555     5
    ## 7 Ahmad    V     Jabbar  987987987 1969-~ 980 Dall~ M      25000 987654321     4
    ## 8 James    E     Borg    888665555 1937-~ 450 Ston~ M      55000 <NA>          1

  9. Mendapatkan data Employee dengan ketentuan tertentu
  10. Filter <- Employee%>%filter(Dno>3 & Salary>25000)%>% #Employee dengan Dno > 3 yang gajinya minimal 25000
                         select(Sex, Salary)%>% #Fokus pada atribut Jenis kelamin dan gaji
                         group_by(Sex) #Kelompokkan berdasarkan jenis kelamin

  11. Menampilkan hasil filter beserta query SQL-nya
  12. Filter; show_query(Filter)
    ## # Source:   lazy query [?? x 2]
    ## # Database: sqlite 3.37.0 [D:\Users\Documents\MANDAREL\Company.db]
    ## # Groups:   Sex
    ##   Sex   Salary
    ##   <chr>  <int>
    ## 1 M      30000
    ## 2 M      40000
    ## 3 F      43000
    ## 4 M      38000
    ## <SQL>
    ## SELECT `Sex`, `Salary`
    ## FROM `EMPLOYEE`
    ## WHERE (`Dno` > 3.0 AND `Salary` > 25000.0)

  13. Melakukan spesifikasi (refilter) data yang ingin diamati
  14. Hasil <- Filter%>%summarize(Jumlah_Pegawai=count(Salary), #Dapatkan jumlah pegawai berdasarkan banyak record gaji
                         Rataan_Gaji_Pegawai_Dept5=mean(Salary)); #Kemudian dihitung rataan gaji pegawai

  15. Setelah wrangling dilakukan, simpan ke dalam objek Hasil, lalu tampilkan beserta query SQL-nya
  16. Hasil; show_query(Hasil) 
    ## Warning: Missing values are always removed in SQL.
    ## Use `mean(x, na.rm = TRUE)` to silence this warning
    ## This warning is displayed only once per session.
    ## # Source:   lazy query [?? x 3]
    ## # Database: sqlite 3.37.0 [D:\Users\Documents\MANDAREL\Company.db]
    ##   Sex   Jumlah_Pegawai Rataan_Gaji_Pegawai_Dept5
    ##   <chr>          <int>                     <dbl>
    ## 1 F                  1                     43000
    ## 2 M                  3                     36000
    ## <SQL>
    ## SELECT `Sex`, count(`Salary`) AS `Jumlah_Pegawai`, AVG(`Salary`) AS `Rataan_Gaji_Pegawai_Dept5`
    ## FROM (SELECT `Sex`, `Salary`
    ## FROM `EMPLOYEE`
    ## WHERE (`Dno` > 3.0 AND `Salary` > 25000.0))
    ## GROUP BY `Sex`

C. Akses dan Wrangling Dataset di R dengan dplyr ~

  1. Mengakses data CO2 yang ada pada paket dataset di R
  2. head(datasets::CO2)
    ##   Plant   Type  Treatment conc uptake
    ## 1   Qn1 Quebec nonchilled   95   16.0
    ## 2   Qn1 Quebec nonchilled  175   30.4
    ## 3   Qn1 Quebec nonchilled  250   34.8
    ## 4   Qn1 Quebec nonchilled  350   37.2
    ## 5   Qn1 Quebec nonchilled  500   35.3
    ## 6   Qn1 Quebec nonchilled  675   39.2

  3. Memfilter Data CO2 dengan Tipe Mississipi dan Treatment chilled
  4. Filter.Mississippi.Chilled <- CO2%>%filter(Type=="Mississippi"& Treatment=="chilled")
    Filter.Mississippi.Chilled
    ##    Plant        Type Treatment conc uptake
    ## 1    Mc1 Mississippi   chilled   95   10.5
    ## 2    Mc1 Mississippi   chilled  175   14.9
    ## 3    Mc1 Mississippi   chilled  250   18.1
    ## 4    Mc1 Mississippi   chilled  350   18.9
    ## 5    Mc1 Mississippi   chilled  500   19.5
    ## 6    Mc1 Mississippi   chilled  675   22.2
    ## 7    Mc1 Mississippi   chilled 1000   21.9
    ## 8    Mc2 Mississippi   chilled   95    7.7
    ## 9    Mc2 Mississippi   chilled  175   11.4
    ## 10   Mc2 Mississippi   chilled  250   12.3
    ## 11   Mc2 Mississippi   chilled  350   13.0
    ## 12   Mc2 Mississippi   chilled  500   12.5
    ## 13   Mc2 Mississippi   chilled  675   13.7
    ## 14   Mc2 Mississippi   chilled 1000   14.4
    ## 15   Mc3 Mississippi   chilled   95   10.6
    ## 16   Mc3 Mississippi   chilled  175   18.0
    ## 17   Mc3 Mississippi   chilled  250   17.9
    ## 18   Mc3 Mississippi   chilled  350   17.9
    ## 19   Mc3 Mississippi   chilled  500   17.9
    ## 20   Mc3 Mississippi   chilled  675   18.9
    ## 21   Mc3 Mississippi   chilled 1000   19.9

  5. Memilih Objek Filter.Missippi tanpa variabel Type dan Treatment
  6. Unselect.Type.Treatment <- Filter.Mississippi.Chilled%>%select(-Type, -Treatment)

  7. Memeriksa summary dari variabel Plant untuk mengetahui tanaman yang ada sesuai filter
  8. summary(Unselect.Type.Treatment$Plant)
    ## Qn1 Qn2 Qn3 Qc1 Qc3 Qc2 Mn3 Mn2 Mn1 Mc2 Mc3 Mc1 
    ##   0   0   0   0   0   0   0   0   0   7   7   7

  9. Objek Unselect.Type.Treatment dikelompokkan berdasarkan variabel Plant, kemudian diurutkan secara ascending berdasarkan variabel conc dan uptake
  10. Sorted.Grouped.Data <- Unselect.Type.Treatment%>%group_by(Plant)%>%arrange(conc & uptake)
    Sorted.Grouped.Data
    ## # A tibble: 21 x 3
    ## # Groups:   Plant [3]
    ##    Plant  conc uptake
    ##    <ord> <dbl>  <dbl>
    ##  1 Mc1      95   10.5
    ##  2 Mc1     175   14.9
    ##  3 Mc1     250   18.1
    ##  4 Mc1     350   18.9
    ##  5 Mc1     500   19.5
    ##  6 Mc1     675   22.2
    ##  7 Mc1    1000   21.9
    ##  8 Mc2      95    7.7
    ##  9 Mc2     175   11.4
    ## 10 Mc2     250   12.3
    ## # ... with 11 more rows

  11. Wrangling Data gabungan
  12. CO2.new <-CO2%>%filter(Type=="Mississippi" & Treatment=="chilled")%>%select(-Type, -Treatment)%>%group_by(Plant)%>%arrange(conc & uptake); CO2.new
    ## # A tibble: 21 x 3
    ## # Groups:   Plant [3]
    ##    Plant  conc uptake
    ##    <ord> <dbl>  <dbl>
    ##  1 Mc1      95   10.5
    ##  2 Mc1     175   14.9
    ##  3 Mc1     250   18.1
    ##  4 Mc1     350   18.9
    ##  5 Mc1     500   19.5
    ##  6 Mc1     675   22.2
    ##  7 Mc1    1000   21.9
    ##  8 Mc2      95    7.7
    ##  9 Mc2     175   11.4
    ## 10 Mc2     250   12.3
    ## # ... with 11 more rows

  13. Penggunaan fungsi summarise() pada paket dplyr() untuk menghitung rataan dari varibel conc dan uptake untuk masing-masing Plant setelah dibulatkan
  14. Sub1 <- CO2.new%>%filter(Plant=="Mc1")%>%summarise(Mean.conc=mean(conc), Mean.uptake = round(mean(uptake))); Sub1
    ## # A tibble: 1 x 3
    ##   Plant Mean.conc Mean.uptake
    ##   <ord>     <dbl>       <dbl>
    ## 1 Mc1         435          18
    Sub2 <- CO2.new%>%filter(Plant=="Mc2")%>%summarise(Mean.conc=mean(conc), Mean.uptake = round(mean(uptake))); Sub2
    ## # A tibble: 1 x 3
    ##   Plant Mean.conc Mean.uptake
    ##   <ord>     <dbl>       <dbl>
    ## 1 Mc2         435          12
    Sub3 <- CO2.new%>%filter(Plant=="Mc3")%>%summarise(Mean.conc=mean(conc), Mean.uptake = round(mean(uptake))); Sub3
    ## # A tibble: 1 x 3
    ##   Plant Mean.conc Mean.uptake
    ##   <ord>     <dbl>       <dbl>
    ## 1 Mc3         435          17

  15. Ketiga sub di atas disatukan sebagai record dalam objek baru yang bernama CO2.NEW, disertai dengan penamaan kolom
  16. CO2.NEW <- rbind(Sub1, Sub2, Sub3); CO2.NEW
    ## # A tibble: 3 x 3
    ##   Plant Mean.conc Mean.uptake
    ##   <ord>     <dbl>       <dbl>
    ## 1 Mc1         435          18
    ## 2 Mc2         435          12
    ## 3 Mc3         435          17
    names(CO2.NEW) <- c("Tanaman","Rataan Konsentrasi", "Rataan Serapan"); CO2.NEW
    ## # A tibble: 3 x 3
    ##   Tanaman `Rataan Konsentrasi` `Rataan Serapan`
    ##   <ord>                  <dbl>            <dbl>
    ## 1 Mc1                      435               18
    ## 2 Mc2                      435               12
    ## 3 Mc3                      435               17

  17. Menyempurnakan tahap akhir sebagai berikut
  18. #Menambahkan peubah baru bernama Rasio.Serapan yang merupakan hasil dari perbandingan rataan serapan terhadap konsentrasi CO2 pada tanaman. Data diurutkan secara descending berdasarkan Rasio.Serapan, lalu menghilangkan atribut Rataan Konsentrasi dengan Rataan Serapan dan disimpan dalam objek final dengan nama Data.CO2
    Data.CO2 <- CO2.NEW%>%mutate(Rasio.Serapan.CO2=round(`Rataan Serapan`/`Rataan Konsentrasi`, 3))%>%select(-`Rataan Konsentrasi`,-`Rataan Serapan`)%>%arrange(desc(Rasio.Serapan.CO2));Data.CO2
    ## # A tibble: 3 x 2
    ##   Tanaman Rasio.Serapan.CO2
    ##   <ord>               <dbl>
    ## 1 Mc1                 0.041
    ## 2 Mc3                 0.039
    ## 3 Mc2                 0.028