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 'stringr' 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.1
## ✔ 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
library(datasets)
data(Orange)
Orange <- tibble::as_tibble(Orange)
#melihat tipe data
class(Orange)
## [1] "tbl_df"     "tbl"        "data.frame"
#melihat tampilan data dari airquality
View(Orange)
#melihat ringkasan dari data
head(Orange)
## # A tibble: 6 × 3
##   Tree    age circumference
##   <ord> <dbl>         <dbl>
## 1 1       118            30
## 2 1       484            58
## 3 1       664            87
## 4 1      1004           115
## 5 1      1231           120
## 6 1      1372           142
glimpse(Orange)
## Rows: 35
## Columns: 3
## $ Tree          <ord> 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3,…
## $ age           <dbl> 118, 484, 664, 1004, 1231, 1372, 1582, 118, 484, 664, 10…
## $ circumference <dbl> 30, 58, 87, 115, 120, 142, 145, 33, 69, 111, 156, 172, 2…
#menghitung rata-rata umur pohon
mean(Orange$age)
## [1] 922.1429
#Setelah memanggil %>% (dilanjutkan) menghitung mean
mean(Orange$age) == Orange$age %>% mean()
## [1] TRUE
#memasukkan data
C <- c(30, 58, 87, 115, 120, 142, 145, 33, 69)
C
## [1]  30  58  87 115 120 142 145  33  69
#dilogaritmakan, didiferensialkan, dieksponensialkan, dibulatkan 1 angka dibelakang koma
round(exp(diff(log(C))),1)
## [1] 1.9 1.5 1.3 1.0 1.2 1.0 0.2 2.1
C %>% log () %>%
  diff () %>%
  exp () %>%
  round (1)
## [1] 1.9 1.5 1.3 1.0 1.2 1.0 0.2 2.1
#summarize 
##Menghitung rata-rata kecepatan angin setiap bulan
Orange %>% group_by(Tree) %>% summarize (mean=mean(circumference))
## # A tibble: 5 × 2
##   Tree   mean
##   <ord> <dbl>
## 1 3      94  
## 2 1      99.6
## 3 5     111. 
## 4 2     135. 
## 5 4     139.
##Mengurutkkan berdasarkan peubah circumference dengan ascending
Orange %>% arrange(circumference)
## # A tibble: 35 × 3
##    Tree    age circumference
##    <ord> <dbl>         <dbl>
##  1 1       118            30
##  2 3       118            30
##  3 5       118            30
##  4 4       118            32
##  5 2       118            33
##  6 5       484            49
##  7 3       484            51
##  8 1       484            58
##  9 4       484            62
## 10 2       484            69
## # ℹ 25 more rows
##Mengurutkkan berdasarkan peubah circumference dengan descending
Orange %>% arrange (desc(circumference))
## # A tibble: 35 × 3
##    Tree    age circumference
##    <ord> <dbl>         <dbl>
##  1 4      1582           214
##  2 4      1372           209
##  3 2      1372           203
##  4 2      1582           203
##  5 4      1231           179
##  6 5      1582           177
##  7 5      1372           174
##  8 2      1231           172
##  9 4      1004           167
## 10 2      1004           156
## # ℹ 25 more rows

#Filter

##Ngambil data yang pada ordo 4
Orange %>% filter(Tree=="4")
## # A tibble: 7 × 3
##   Tree    age circumference
##   <ord> <dbl>         <dbl>
## 1 4       118            32
## 2 4       484            62
## 3 4       664           112
## 4 4      1004           167
## 5 4      1231           179
## 6 4      1372           209
## 7 4      1582           214
##Ngambil data yang age, circumference
Orange %>% select(age,circumference)
## # A tibble: 35 × 2
##      age circumference
##    <dbl>         <dbl>
##  1   118            30
##  2   484            58
##  3   664            87
##  4  1004           115
##  5  1231           120
##  6  1372           142
##  7  1582           145
##  8   118            33
##  9   484            69
## 10   664           111
## # ℹ 25 more rows
##Ngambil data yang bukan Tree dan bukan age
Orange %>% select(-Tree,-age)
## # A tibble: 35 × 1
##    circumference
##            <dbl>
##  1            30
##  2            58
##  3            87
##  4           115
##  5           120
##  6           142
##  7           145
##  8            33
##  9            69
## 10           111
## # ℹ 25 more rows
#Mutate = menambah peubah baru
##menghitung rasio circumference dan age
Orange %>% mutate(Ratio=Orange$age / Orange$circumference)
## # A tibble: 35 × 4
##    Tree    age circumference Ratio
##    <ord> <dbl>         <dbl> <dbl>
##  1 1       118            30  3.93
##  2 1       484            58  8.34
##  3 1       664            87  7.63
##  4 1      1004           115  8.73
##  5 1      1231           120 10.3 
##  6 1      1372           142  9.66
##  7 1      1582           145 10.9 
##  8 2       118            33  3.58
##  9 2       484            69  7.01
## 10 2       664           111  5.98
## # ℹ 25 more rows
##menghilangkan peubah Tree, lalu memasukkan peubah ratio
Orangebaru <- Orange %>% select(-Tree) %>% mutate(Ratio=Orange$age / Orange$circumference)
Orangebaru
## # A tibble: 35 × 3
##      age circumference Ratio
##    <dbl>         <dbl> <dbl>
##  1   118            30  3.93
##  2   484            58  8.34
##  3   664            87  7.63
##  4  1004           115  8.73
##  5  1231           120 10.3 
##  6  1372           142  9.66
##  7  1582           145 10.9 
##  8   118            33  3.58
##  9   484            69  7.01
## 10   664           111  5.98
## # ℹ 25 more rows