library(datasets)
library(tidyverse)
## Warning: package 'tidyverse' was built under R version 4.3.2
## Warning: package 'dplyr' 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.2     ✔ tibble    3.2.1
## ✔ lubridate 1.9.2     ✔ 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
#memanggil data rivers
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.
#Memberikan ringkasan atau tampilan yang lebih lengkap tentang struktur suatu dataset
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, …
#Menampilkan beberapa baris pertama dari suatu objek
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
#Mendapatkan atau menetapkan kelas objek
class(CO2)
## [1] "tbl_df"     "tbl"        "data.frame"
#Menghitung Rata-rata conc
mean(CO2$conc)
## [1] 435
#mengurutkan peubah conc dari yang terkecil
CO2 %>% arrange(conc)
## # A tibble: 84 × 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
##  7 Mn1   Mississippi nonchilled    95   10.6
##  8 Mn2   Mississippi nonchilled    95   12  
##  9 Mn3   Mississippi nonchilled    95   11.3
## 10 Mc1   Mississippi chilled       95   10.5
## # ℹ 74 more rows
#Mengurutkan berdasarkan peubah conc dari nilai terbesar
CO2 %>% arrange(desc(conc))
## # A tibble: 84 × 5
##    Plant Type        Treatment   conc uptake
##    <ord> <fct>       <fct>      <dbl>  <dbl>
##  1 Qn1   Quebec      nonchilled  1000   39.7
##  2 Qn2   Quebec      nonchilled  1000   44.3
##  3 Qn3   Quebec      nonchilled  1000   45.5
##  4 Qc1   Quebec      chilled     1000   38.7
##  5 Qc2   Quebec      chilled     1000   42.4
##  6 Qc3   Quebec      chilled     1000   41.4
##  7 Mn1   Mississippi nonchilled  1000   35.5
##  8 Mn2   Mississippi nonchilled  1000   31.5
##  9 Mn3   Mississippi nonchilled  1000   27.8
## 10 Mc1   Mississippi chilled     1000   21.9
## # ℹ 74 more rows
#menghapus kolom quebec
CO2 %>% filter(Type=="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
 #memilih kolom plant dan conc
CO2 %>% select(Plant,conc,Treatment)
## # A tibble: 84 × 3
##    Plant  conc Treatment 
##    <ord> <dbl> <fct>     
##  1 Qn1      95 nonchilled
##  2 Qn1     175 nonchilled
##  3 Qn1     250 nonchilled
##  4 Qn1     350 nonchilled
##  5 Qn1     500 nonchilled
##  6 Qn1     675 nonchilled
##  7 Qn1    1000 nonchilled
##  8 Qn2      95 nonchilled
##  9 Qn2     175 nonchilled
## 10 Qn2     250 nonchilled
## # ℹ 74 more rows
#menghapus kolom conc dan uptake
CO2 %>% select(-conc,-uptake)
## # A tibble: 84 × 3
##    Plant Type   Treatment 
##    <ord> <fct>  <fct>     
##  1 Qn1   Quebec nonchilled
##  2 Qn1   Quebec nonchilled
##  3 Qn1   Quebec nonchilled
##  4 Qn1   Quebec nonchilled
##  5 Qn1   Quebec nonchilled
##  6 Qn1   Quebec nonchilled
##  7 Qn1   Quebec nonchilled
##  8 Qn2   Quebec nonchilled
##  9 Qn2   Quebec nonchilled
## 10 Qn2   Quebec nonchilled
## # ℹ 74 more rows
#Mutasi
CO2 %>% mutate(conup=conc+uptake)
## # A tibble: 84 × 6
##    Plant Type   Treatment   conc uptake conup
##    <ord> <fct>  <fct>      <dbl>  <dbl> <dbl>
##  1 Qn1   Quebec nonchilled    95   16    111 
##  2 Qn1   Quebec nonchilled   175   30.4  205.
##  3 Qn1   Quebec nonchilled   250   34.8  285.
##  4 Qn1   Quebec nonchilled   350   37.2  387.
##  5 Qn1   Quebec nonchilled   500   35.3  535.
##  6 Qn1   Quebec nonchilled   675   39.2  714.
##  7 Qn1   Quebec nonchilled  1000   39.7 1040.
##  8 Qn2   Quebec nonchilled    95   13.6  109.
##  9 Qn2   Quebec nonchilled   175   27.3  202.
## 10 Qn2   Quebec nonchilled   250   37.1  287.
## # ℹ 74 more rows
CO2BARU <- CO2 %>% select(-Plant,-Treatment) %>% mutate(conup=conc+uptake)
View(CO2BARU)
#Menghitung rata-rata Sepal Length setiap species
CO2 %>% group_by(Plant) %>% summarize(mean=mean(conc))
## # 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