#Data
library(tidyverse)
## Warning: package 'tidyverse' 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.3 ✔ 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
library(datasets)
data(Orange)
Orange <- tibble::as.tibble(Orange)
## 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(Orange)
## [1] "tbl_df" "tbl" "data.frame"
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…
#Rata-rata Umur Pohon Jeruk ()
mean(Orange$age)
## [1] 922.1429
mean(Orange$age) == Orange$age %>% mean ()
## [1] TRUE
x <- c(0.109, 0.359, 0.63, 0.996, 0.515, 0.142, 0.017, 0.829, 0.907)
x
## [1] 0.109 0.359 0.630 0.996 0.515 0.142 0.017 0.829 0.907
round(exp(diff(log(x))),1)
## [1] 3.3 1.8 1.6 0.5 0.3 0.1 48.8 1.1
x %>% log() %>%
diff() %>%
exp() %>%
round(1)
## [1] 3.3 1.8 1.6 0.5 0.3 0.1 48.8 1.1
#Umur Pohon Jeruk (Summarize & Arrange)
## Menghitung rata-rata Umur Pohon Jeruk (Hari)
Orange %>% summarize(mean=mean(age))
## # A tibble: 1 × 1
## mean
## <dbl>
## 1 922.
## Mengurutkan berdasarkan peubah Age dari nilai terkecil
Orange %>% arrange(age) %>% print(n=35)
## # A tibble: 35 × 3
## Tree age circumference
## <ord> <dbl> <dbl>
## 1 1 118 30
## 2 2 118 33
## 3 3 118 30
## 4 4 118 32
## 5 5 118 30
## 6 1 484 58
## 7 2 484 69
## 8 3 484 51
## 9 4 484 62
## 10 5 484 49
## 11 1 664 87
## 12 2 664 111
## 13 3 664 75
## 14 4 664 112
## 15 5 664 81
## 16 1 1004 115
## 17 2 1004 156
## 18 3 1004 108
## 19 4 1004 167
## 20 5 1004 125
## 21 1 1231 120
## 22 2 1231 172
## 23 3 1231 115
## 24 4 1231 179
## 25 5 1231 142
## 26 1 1372 142
## 27 2 1372 203
## 28 3 1372 139
## 29 4 1372 209
## 30 5 1372 174
## 31 1 1582 145
## 32 2 1582 203
## 33 3 1582 140
## 34 4 1582 214
## 35 5 1582 177
## Mengurutkan berdasarkan peubah Age dari nilai terbesar
Orange %>% arrange(desc(age)) %>% print(n=35)
## # A tibble: 35 × 3
## Tree age circumference
## <ord> <dbl> <dbl>
## 1 1 1582 145
## 2 2 1582 203
## 3 3 1582 140
## 4 4 1582 214
## 5 5 1582 177
## 6 1 1372 142
## 7 2 1372 203
## 8 3 1372 139
## 9 4 1372 209
## 10 5 1372 174
## 11 1 1231 120
## 12 2 1231 172
## 13 3 1231 115
## 14 4 1231 179
## 15 5 1231 142
## 16 1 1004 115
## 17 2 1004 156
## 18 3 1004 108
## 19 4 1004 167
## 20 5 1004 125
## 21 1 664 87
## 22 2 664 111
## 23 3 664 75
## 24 4 664 112
## 25 5 664 81
## 26 1 484 58
## 27 2 484 69
## 28 3 484 51
## 29 4 484 62
## 30 5 484 49
## 31 1 118 30
## 32 2 118 33
## 33 3 118 30
## 34 4 118 32
## 35 5 118 30
#Keliling Pohon Jeruk (Summarize & Arrange)
## Menghitung rata-rata Keliling Pohon Jeruk (mm)
Orange %>% summarize(mean=mean(circumference))
## # A tibble: 1 × 1
## mean
## <dbl>
## 1 116.
## Mengurutkan berdasarkan peubah circumference dari nilai terkecil
Orange %>% arrange(circumference) %>% print(n=35)
## # 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
## 11 3 664 75
## 12 5 664 81
## 13 1 664 87
## 14 3 1004 108
## 15 2 664 111
## 16 4 664 112
## 17 1 1004 115
## 18 3 1231 115
## 19 1 1231 120
## 20 5 1004 125
## 21 3 1372 139
## 22 3 1582 140
## 23 1 1372 142
## 24 5 1231 142
## 25 1 1582 145
## 26 2 1004 156
## 27 4 1004 167
## 28 2 1231 172
## 29 5 1372 174
## 30 5 1582 177
## 31 4 1231 179
## 32 2 1372 203
## 33 2 1582 203
## 34 4 1372 209
## 35 4 1582 214
## Mengurutkan berdasarkan peubah circumference dari nilai terbesar
Orange %>% arrange(desc(circumference)) %>% print(n=35)
## # 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
## 11 1 1582 145
## 12 1 1372 142
## 13 5 1231 142
## 14 3 1582 140
## 15 3 1372 139
## 16 5 1004 125
## 17 1 1231 120
## 18 1 1004 115
## 19 3 1231 115
## 20 4 664 112
## 21 2 664 111
## 22 3 1004 108
## 23 1 664 87
## 24 5 664 81
## 25 3 664 75
## 26 2 484 69
## 27 4 484 62
## 28 1 484 58
## 29 3 484 51
## 30 5 484 49
## 31 2 118 33
## 32 4 118 32
## 33 1 118 30
## 34 3 118 30
## 35 5 118 30
#Tipe Pohon Jeruk (Filter & Select)
#Filter Memilih Jenis Pohon Jeruk berdasarkan tipe (Contoh: Tipe 1)
Orange %>% filter(Tree=="1")
## # A tibble: 7 × 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
## 7 1 1582 145
Orange %>% select(Tree,circumference)
## # A tibble: 35 × 2
## Tree circumference
## <ord> <dbl>
## 1 1 30
## 2 1 58
## 3 1 87
## 4 1 115
## 5 1 120
## 6 1 142
## 7 1 145
## 8 2 33
## 9 2 69
## 10 2 111
## # ℹ 25 more rows
Orange %>% select(-Tree, -circumference)
## # A tibble: 35 × 1
## age
## <dbl>
## 1 118
## 2 484
## 3 664
## 4 1004
## 5 1231
## 6 1372
## 7 1582
## 8 118
## 9 484
## 10 664
## # ℹ 25 more rows
#Laju Pertumbuhan Keliling Pohon Jeruk (Mutate)
OrangeTree <- Orange %>% mutate(circum.growth=circumference/age)
OrangeTree %>% print(n=35)
## # A tibble: 35 × 4
## Tree age circumference circum.growth
## <ord> <dbl> <dbl> <dbl>
## 1 1 118 30 0.254
## 2 1 484 58 0.120
## 3 1 664 87 0.131
## 4 1 1004 115 0.115
## 5 1 1231 120 0.0975
## 6 1 1372 142 0.103
## 7 1 1582 145 0.0917
## 8 2 118 33 0.280
## 9 2 484 69 0.143
## 10 2 664 111 0.167
## 11 2 1004 156 0.155
## 12 2 1231 172 0.140
## 13 2 1372 203 0.148
## 14 2 1582 203 0.128
## 15 3 118 30 0.254
## 16 3 484 51 0.105
## 17 3 664 75 0.113
## 18 3 1004 108 0.108
## 19 3 1231 115 0.0934
## 20 3 1372 139 0.101
## 21 3 1582 140 0.0885
## 22 4 118 32 0.271
## 23 4 484 62 0.128
## 24 4 664 112 0.169
## 25 4 1004 167 0.166
## 26 4 1231 179 0.145
## 27 4 1372 209 0.152
## 28 4 1582 214 0.135
## 29 5 118 30 0.254
## 30 5 484 49 0.101
## 31 5 664 81 0.122
## 32 5 1004 125 0.125
## 33 5 1231 142 0.115
## 34 5 1372 174 0.127
## 35 5 1582 177 0.112