library(dplyr)
## 
## 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
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ forcats   1.0.0     ✔ readr     2.1.5
## ✔ ggplot2   3.5.1     ✔ stringr   1.5.1
## ✔ lubridate 1.9.3     ✔ tibble    3.2.1
## ✔ purrr     1.0.2     ✔ tidyr     1.3.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors

Menampilkan data

Menggunakan Dataset CO2 Bawaan dari R

data(CO2)
CO2 <- tibble::as_tibble(CO2)

Menampilkan tabel keseluruhan data di tab baru

View(CO2)

Menampilkan 6 baris teratas

head(CO2)
## # A tibble: 6 × 5
##   Plant Type   Treatment   conc uptake
##   <ord> <fct>  <fct>      <dbl>  <dbl>
## 1 Qn1   Quebec nonchilled    95   16  
## 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

Menampilkan tabel dengan lengkap

print(CO2)
## # A tibble: 84 × 5
##    Plant Type   Treatment   conc uptake
##    <ord> <fct>  <fct>      <dbl>  <dbl>
##  1 Qn1   Quebec nonchilled    95   16  
##  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
##  7 Qn1   Quebec nonchilled  1000   39.7
##  8 Qn2   Quebec nonchilled    95   13.6
##  9 Qn2   Quebec nonchilled   175   27.3
## 10 Qn2   Quebec nonchilled   250   37.1
## # ℹ 74 more rows

Menampilkan struktur data secara ringkas dalam format horizontal

glimpse(CO2)
## Rows: 84
## Columns: 5
## $ Plant     <ord> Qn1, Qn1, Qn1, Qn1, Qn1, Qn1, Qn1, Qn2, Qn2, Qn2, Qn2, Qn2, …
## $ Type      <fct> Quebec, Quebec, Quebec, Quebec, Quebec, Quebec, Quebec, Queb…
## $ Treatment <fct> nonchilled, nonchilled, nonchilled, nonchilled, nonchilled, …
## $ conc      <dbl> 95, 175, 250, 350, 500, 675, 1000, 95, 175, 250, 350, 500, 6…
## $ uptake    <dbl> 16.0, 30.4, 34.8, 37.2, 35.3, 39.2, 39.7, 13.6, 27.3, 37.1, …

Penggunaan Satu Fungsi

Select

Mengambil data dari beberapa kolom, yaitu Plant, Treatment, conc, dan uptake

CO2_Selected <-CO2 %>%    
  select(Plant, Treatment, conc, uptake) 
CO2_Selected
## # A tibble: 84 × 4
##    Plant Treatment   conc uptake
##    <ord> <fct>      <dbl>  <dbl>
##  1 Qn1   nonchilled    95   16  
##  2 Qn1   nonchilled   175   30.4
##  3 Qn1   nonchilled   250   34.8
##  4 Qn1   nonchilled   350   37.2
##  5 Qn1   nonchilled   500   35.3
##  6 Qn1   nonchilled   675   39.2
##  7 Qn1   nonchilled  1000   39.7
##  8 Qn2   nonchilled    95   13.6
##  9 Qn2   nonchilled   175   27.3
## 10 Qn2   nonchilled   250   37.1
## # ℹ 74 more rows

Filtering

Mengambil data dengan konsentrasi CO2 (conc) yang nilainya lebih besar dari 500

CO2_filtered <- CO2 %>% 
  filter(conc > 500)
CO2_filtered
## # A tibble: 24 × 5
##    Plant Type   Treatment   conc uptake
##    <ord> <fct>  <fct>      <dbl>  <dbl>
##  1 Qn1   Quebec nonchilled   675   39.2
##  2 Qn1   Quebec nonchilled  1000   39.7
##  3 Qn2   Quebec nonchilled   675   41.4
##  4 Qn2   Quebec nonchilled  1000   44.3
##  5 Qn3   Quebec nonchilled   675   43.9
##  6 Qn3   Quebec nonchilled  1000   45.5
##  7 Qc1   Quebec chilled      675   35.4
##  8 Qc1   Quebec chilled     1000   38.7
##  9 Qc2   Quebec chilled      675   37.5
## 10 Qc2   Quebec chilled     1000   42.4
## # ℹ 14 more rows

Mengambil data hanya untuk tanaman dengan Type Quebec

CO2_Quebec <- CO2 %>% 
  filter(Type == "Quebec")
CO2_Quebec
## # A tibble: 42 × 5
##    Plant Type   Treatment   conc uptake
##    <ord> <fct>  <fct>      <dbl>  <dbl>
##  1 Qn1   Quebec nonchilled    95   16  
##  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
##  7 Qn1   Quebec nonchilled  1000   39.7
##  8 Qn2   Quebec nonchilled    95   13.6
##  9 Qn2   Quebec nonchilled   175   27.3
## 10 Qn2   Quebec nonchilled   250   37.1
## # ℹ 32 more rows

Arranging

Mengurutkan data berdasarkan laju penyerapan tertinggi

Menampilkan 10 data teratas berdasarkan konsentrasi CO2 terendah dan laju penyerapan tertinggi

arr1 <- CO2 %>% arrange (desc(uptake)) %>% slice_head (n = 10)
arr1
## # A tibble: 10 × 5
##    Plant Type   Treatment   conc uptake
##    <ord> <fct>  <fct>      <dbl>  <dbl>
##  1 Qn3   Quebec nonchilled  1000   45.5
##  2 Qn2   Quebec nonchilled  1000   44.3
##  3 Qn3   Quebec nonchilled   675   43.9
##  4 Qn3   Quebec nonchilled   500   42.9
##  5 Qc2   Quebec chilled     1000   42.4
##  6 Qn3   Quebec nonchilled   350   42.1
##  7 Qn2   Quebec nonchilled   350   41.8
##  8 Qn2   Quebec nonchilled   675   41.4
##  9 Qc3   Quebec chilled     1000   41.4
## 10 Qn2   Quebec nonchilled   500   40.6

Mengurutkan data berdasarkan konsentrasi CO2 terendah dan laju penyerapan tertinggi

Menampilkan 5 data teratas berdasarkan konsentrasi CO2 terendah dan laju penyerapan tertinggi

arr2 <- CO2 %>% arrange (conc,desc(uptake)) %>% slice_head (n = 5)
arr2
## # A tibble: 5 × 5
##   Plant Type   Treatment   conc uptake
##   <ord> <fct>  <fct>      <dbl>  <dbl>
## 1 Qn3   Quebec nonchilled    95   16.2
## 2 Qn1   Quebec nonchilled    95   16  
## 3 Qc3   Quebec chilled       95   15.1
## 4 Qc1   Quebec chilled       95   14.2
## 5 Qn2   Quebec nonchilled    95   13.6

Summarize

Menampilkan rata rata konsentrasi CO2 per tanamannya

summarize_CO2 <- CO2 %>% 
  group_by(Plant) %>% 
  summarize(mean=mean(conc))
summarize_CO2
## # A tibble: 12 × 2
##    Plant  mean
##    <ord> <dbl>
##  1 Qn1     435
##  2 Qn2     435
##  3 Qn3     435
##  4 Qc1     435
##  5 Qc3     435
##  6 Qc2     435
##  7 Mn3     435
##  8 Mn2     435
##  9 Mn1     435
## 10 Mc2     435
## 11 Mc3     435
## 12 Mc1     435

Mutate

Menambah beberapa kolom baru, yaitu untuk konsentrasi (conc) dengan satuannya menjadi ppm/100 dan

CO2_mutate <- CO2 %>% mutate (new_conc = conc / 100)
head(CO2_mutate)
## # A tibble: 6 × 6
##   Plant Type   Treatment   conc uptake new_conc
##   <ord> <fct>  <fct>      <dbl>  <dbl>    <dbl>
## 1 Qn1   Quebec nonchilled    95   16       0.95
## 2 Qn1   Quebec nonchilled   175   30.4     1.75
## 3 Qn1   Quebec nonchilled   250   34.8     2.5 
## 4 Qn1   Quebec nonchilled   350   37.2     3.5 
## 5 Qn1   Quebec nonchilled   500   35.3     5   
## 6 Qn1   Quebec nonchilled   675   39.2     6.75

Menambah beberapa kolom baru, yaitu untuk konsentrasi (conc) dengan satuannya menjadi ppm/100 dan

new_data <- CO2 %>%
  mutate(conc_category = case_when(
         conc < 300 ~ "low",
         conc >= 300 & conc <= 600~ "medium",
         conc > 600 ~ "high"
  ))
print(new_data, 15)
## # A tibble: 84
## #   × 6
##    Plant Type  
##    <ord> <fct> 
##  1 Qn1   Quebec
##  2 Qn1   Quebec
##  3 Qn1   Quebec
##  4 Qn1   Quebec
##  5 Qn1   Quebec
##  6 Qn1   Quebec
##  7 Qn1   Quebec
##  8 Qn2   Quebec
##  9 Qn2   Quebec
## 10 Qn2   Quebec
## # ℹ 74 more
## #   rows
## # ℹ 4 more
## #   variables:
## #   Treatment <fct>,
## #   conc <dbl>,
## #   uptake <dbl>, …

Penggunaan Dua Fungsi secara Bersamaan

Menampilkan data dengan laju penyerapan yang lebih tinggi dari rata-rata keseluruhan

mean_uptake <- mean(CO2$uptake)
CO2 %>% filter (uptake > mean_uptake)
## # A tibble: 47 × 5
##    Plant Type   Treatment   conc uptake
##    <ord> <fct>  <fct>      <dbl>  <dbl>
##  1 Qn1   Quebec nonchilled   175   30.4
##  2 Qn1   Quebec nonchilled   250   34.8
##  3 Qn1   Quebec nonchilled   350   37.2
##  4 Qn1   Quebec nonchilled   500   35.3
##  5 Qn1   Quebec nonchilled   675   39.2
##  6 Qn1   Quebec nonchilled  1000   39.7
##  7 Qn2   Quebec nonchilled   175   27.3
##  8 Qn2   Quebec nonchilled   250   37.1
##  9 Qn2   Quebec nonchilled   350   41.8
## 10 Qn2   Quebec nonchilled   500   40.6
## # ℹ 37 more rows

Menampilkan data dengan konsentrasi CO2 yang lebih tinggi dari rata-rata keseluruhan

mean_conc <- mean(CO2$conc)
CO2 %>% filter (conc > mean_conc) %>% 
  select(-uptake)
## # A tibble: 36 × 4
##    Plant Type   Treatment   conc
##    <ord> <fct>  <fct>      <dbl>
##  1 Qn1   Quebec nonchilled   500
##  2 Qn1   Quebec nonchilled   675
##  3 Qn1   Quebec nonchilled  1000
##  4 Qn2   Quebec nonchilled   500
##  5 Qn2   Quebec nonchilled   675
##  6 Qn2   Quebec nonchilled  1000
##  7 Qn3   Quebec nonchilled   500
##  8 Qn3   Quebec nonchilled   675
##  9 Qn3   Quebec nonchilled  1000
## 10 Qc1   Quebec chilled      500
## # ℹ 26 more rows