library(tidyverse)
## Warning: package 'tidyverse' was built under R version 4.3.2
## Warning: package 'dplyr' was built under R version 4.3.2
## Warning: package 'lubridate' was built under R version 4.3.2
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.4
## ✔ forcats   1.0.0     ✔ stringr   1.5.0
## ✔ ggplot2   3.4.4     ✔ tibble    3.2.1
## ✔ lubridate 1.9.3     ✔ tidyr     1.3.0
## ✔ purrr     1.0.2     
## ── 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
?tidyverse
## starting httpd help server ... done
library(datasets)
data(CO2)
CO2 <- tibble::as.tibble(CO2)
## Warning: `as.tibble()` was deprecated in tibble 2.0.0.
## ℹ Please use `as_tibble()` instead.
## ℹ The signature and semantics have changed, see `?as_tibble`.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
class(CO2)
## [1] "tbl_df"     "tbl"        "data.frame"
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
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, …

#summarise() # Menghitung rata-rata conc dan uptake

summary_stats <- CO2 %>%
  summarise(mean_conc = mean(conc, na.rm = TRUE),
            mean_uptake = mean(uptake, na.rm = TRUE))
print(summary_stats)
## # A tibble: 1 × 2
##   mean_conc mean_uptake
##       <dbl>       <dbl>
## 1       435        27.2

#arrange() # Mengurutkan data berdasarkan conc secara menaik

sorted_CO2 <- CO2 %>%
  arrange(conc)
print(head(sorted_CO2))
## # A tibble: 6 × 5
##   Plant Type   Treatment   conc uptake
##   <ord> <fct>  <fct>      <dbl>  <dbl>
## 1 Qn1   Quebec nonchilled    95   16  
## 2 Qn2   Quebec nonchilled    95   13.6
## 3 Qn3   Quebec nonchilled    95   16.2
## 4 Qc1   Quebec chilled       95   14.2
## 5 Qc2   Quebec chilled       95    9.3
## 6 Qc3   Quebec chilled       95   15.1

#filter() # Menyaring data dengan uptake di atas rata-rata

filtered_CO2 <- CO2 %>%
  filter(uptake > mean(uptake, na.rm = TRUE))
print(filtered_CO2)
## # 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

#mutate() # Menambahkan kolom baru ‘adjusted_conc’ dengan nilai conc dikali 2 (mutate())

mutated_CO2 <- CO2 %>%
  mutate(adjusted_conc = conc * 2)
print(head(mutated_CO2))
## # A tibble: 6 × 6
##   Plant Type   Treatment   conc uptake adjusted_conc
##   <ord> <fct>  <fct>      <dbl>  <dbl>         <dbl>
## 1 Qn1   Quebec nonchilled    95   16             190
## 2 Qn1   Quebec nonchilled   175   30.4           350
## 3 Qn1   Quebec nonchilled   250   34.8           500
## 4 Qn1   Quebec nonchilled   350   37.2           700
## 5 Qn1   Quebec nonchilled   500   35.3          1000
## 6 Qn1   Quebec nonchilled   675   39.2          1350

#select() # Memilih kolom tertentu

selected_CO2 <- CO2 %>%
  select(Plant, Type, conc, uptake)
print(head(selected_CO2))
## # A tibble: 6 × 4
##   Plant Type    conc uptake
##   <ord> <fct>  <dbl>  <dbl>
## 1 Qn1   Quebec    95   16  
## 2 Qn1   Quebec   175   30.4
## 3 Qn1   Quebec   250   34.8
## 4 Qn1   Quebec   350   37.2
## 5 Qn1   Quebec   500   35.3
## 6 Qn1   Quebec   675   39.2

#Penggunaan secara bersama-sama (Chained) untuk Analisis Sederhana:

result <- CO2 %>%
  filter(Treatment == "chilled") %>%
  arrange(conc) %>%
  mutate(adjusted_uptake = uptake * 1.5) %>%
  select(Plant, Type, conc, uptake, adjusted_uptake) %>%
  summarise(mean_uptake = mean(uptake, na.rm = TRUE),
            max_adj_uptake = max(adjusted_uptake))

print(result)
## # A tibble: 1 × 2
##   mean_uptake max_adj_uptake
##         <dbl>          <dbl>
## 1        23.8           63.6

#Menggabungkan beberapa fungsi secara bersama-sama

combined_operations <- CO2 %>%
  filter(Treatment == "nonchilled") %>%
  arrange(desc(uptake)) %>%
  mutate(adjusted_conc = conc * 2) %>%
  select(Plant, Type, conc, uptake, adjusted_conc) %>%
  summarise(mean_uptake = mean(uptake, na.rm = TRUE),
            max_adj_conc = max(adjusted_conc))
print(combined_operations)
## # A tibble: 1 × 2
##   mean_uptake max_adj_conc
##         <dbl>        <dbl>
## 1        30.6         2000

#Menggabungkan beberapa fungsi secara bersama-sama

advanced_result <- CO2 %>%
  filter(Treatment %in% c("chilled", "nonchilled")) %>%
  arrange(Treatment, conc) %>%
  mutate(adjusted_uptake = ifelse(Treatment == "chilled", uptake * 1.5, uptake * 2)) %>%
  select(Plant, Type, Treatment, conc, uptake, adjusted_uptake) %>%
  group_by(Treatment) %>%
  summarise(mean_uptake = mean(uptake, na.rm = TRUE),
            max_adj_uptake = max(adjusted_uptake),
            total_plants = n())
print(advanced_result)
## # A tibble: 2 × 4
##   Treatment  mean_uptake max_adj_uptake total_plants
##   <fct>            <dbl>          <dbl>        <int>
## 1 nonchilled        30.6           91             42
## 2 chilled           23.8           63.6           42