library(tidyverse)
## Warning: package 'tidyverse' was built under R version 4.3.2
## Warning: package 'readr' was built under R version 4.3.2
## Warning: package 'forcats' 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"
view(Orange)
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…
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
#summarize
## Menghitung rata-rata keliling setiap pohon
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.
## Mengurutkan berdasarkan peubah keliling dari nilai terkecil
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
## Mengurutkan berdasarkan peubah keliling dari nilai terbesar
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
Orange %>% filter(Tree=="3")
## # A tibble: 7 × 3
## Tree age circumference
## <ord> <dbl> <dbl>
## 1 3 118 30
## 2 3 484 51
## 3 3 664 75
## 4 3 1004 108
## 5 3 1231 115
## 6 3 1372 139
## 7 3 1582 140
Orange %>% select(Tree,age)
## # A tibble: 35 × 2
## Tree age
## <ord> <dbl>
## 1 1 118
## 2 1 484
## 3 1 664
## 4 1 1004
## 5 1 1231
## 6 1 1372
## 7 1 1582
## 8 2 118
## 9 2 484
## 10 2 664
## # ℹ 25 more rows
Orange %>% select(-circumference)
## # A tibble: 35 × 2
## Tree age
## <ord> <dbl>
## 1 1 118
## 2 1 484
## 3 1 664
## 4 1 1004
## 5 1 1231
## 6 1 1372
## 7 1 1582
## 8 2 118
## 9 2 484
## 10 2 664
## # ℹ 25 more rows
#Mutate
Orange %>% filter(Tree=="2")
## # A tibble: 7 × 3
## Tree age circumference
## <ord> <dbl> <dbl>
## 1 2 118 33
## 2 2 484 69
## 3 2 664 111
## 4 2 1004 156
## 5 2 1231 172
## 6 2 1372 203
## 7 2 1582 203
newOrange <- Orange %>% mutate(Jumlahhitung=age+circumference)
newOrange
## # A tibble: 35 × 4
## Tree age circumference Jumlahhitung
## <ord> <dbl> <dbl> <dbl>
## 1 1 118 30 148
## 2 1 484 58 542
## 3 1 664 87 751
## 4 1 1004 115 1119
## 5 1 1231 120 1351
## 6 1 1372 142 1514
## 7 1 1582 145 1727
## 8 2 118 33 151
## 9 2 484 69 553
## 10 2 664 111 775
## # ℹ 25 more rows