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
library(datasets)
#Memanggil data Jeruk
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.
#Menetapkan kelas Jeruk
class(Orange)
## [1] "tbl_df" "tbl" "data.frame"
#Menampilkan baris pertama
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
#Membuat ringkasan data
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…
#Summarize
#Menghitung rata-rata umur Jeruk
Orange %>% group_by(Tree) %>% summarize(mean=mean(age)) %>% print(n=35)
## # A tibble: 5 × 2
## Tree mean
## <ord> <dbl>
## 1 3 922.
## 2 1 922.
## 3 5 922.
## 4 2 922.
## 5 4 922.
#Menyusun berdasarkan keliling Jeruk 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
#Menyusun berdasarkan keliling Jeruk 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
#Filter
#Mengambil Jeruk hanya pohon 2
Orange %>% filter(Tree=='2') %>% print(n=35)
## # 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
#Mengambil variabel tertentu
Orange %>% select(Tree,circumference) %>% print(n=35)
## # 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
## 11 2 156
## 12 2 172
## 13 2 203
## 14 2 203
## 15 3 30
## 16 3 51
## 17 3 75
## 18 3 108
## 19 3 115
## 20 3 139
## 21 3 140
## 22 4 32
## 23 4 62
## 24 4 112
## 25 4 167
## 26 4 179
## 27 4 209
## 28 4 214
## 29 5 30
## 30 5 49
## 31 5 81
## 32 5 125
## 33 5 142
## 34 5 174
## 35 5 177
#Menghapus variabel
Orange %>% select(-circumference) %>% print(n=35)
## # 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
## 11 2 1004
## 12 2 1231
## 13 2 1372
## 14 2 1582
## 15 3 118
## 16 3 484
## 17 3 664
## 18 3 1004
## 19 3 1231
## 20 3 1372
## 21 3 1582
## 22 4 118
## 23 4 484
## 24 4 664
## 25 4 1004
## 26 4 1231
## 27 4 1372
## 28 4 1582
## 29 5 118
## 30 5 484
## 31 5 664
## 32 5 1004
## 33 5 1231
## 34 5 1372
## 35 5 1582
#Mutate
#Menambah kolom
Orange %>% mutate(characteristic=circumference+age) %>% print(n=35)
## # A tibble: 35 × 4
## Tree age circumference characteristic
## <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
## 11 2 1004 156 1160
## 12 2 1231 172 1403
## 13 2 1372 203 1575
## 14 2 1582 203 1785
## 15 3 118 30 148
## 16 3 484 51 535
## 17 3 664 75 739
## 18 3 1004 108 1112
## 19 3 1231 115 1346
## 20 3 1372 139 1511
## 21 3 1582 140 1722
## 22 4 118 32 150
## 23 4 484 62 546
## 24 4 664 112 776
## 25 4 1004 167 1171
## 26 4 1231 179 1410
## 27 4 1372 209 1581
## 28 4 1582 214 1796
## 29 5 118 30 148
## 30 5 484 49 533
## 31 5 664 81 745
## 32 5 1004 125 1129
## 33 5 1231 142 1373
## 34 5 1372 174 1546
## 35 5 1582 177 1759
#Menghapus suatu kolom
NewOrange <- Orange %>% select(-Tree) %>% mutate(characteristic=circumference+age) %>% print(n=35)
## # A tibble: 35 × 3
## age circumference characteristic
## <dbl> <dbl> <dbl>
## 1 118 30 148
## 2 484 58 542
## 3 664 87 751
## 4 1004 115 1119
## 5 1231 120 1351
## 6 1372 142 1514
## 7 1582 145 1727
## 8 118 33 151
## 9 484 69 553
## 10 664 111 775
## 11 1004 156 1160
## 12 1231 172 1403
## 13 1372 203 1575
## 14 1582 203 1785
## 15 118 30 148
## 16 484 51 535
## 17 664 75 739
## 18 1004 108 1112
## 19 1231 115 1346
## 20 1372 139 1511
## 21 1582 140 1722
## 22 118 32 150
## 23 484 62 546
## 24 664 112 776
## 25 1004 167 1171
## 26 1231 179 1410
## 27 1372 209 1581
## 28 1582 214 1796
## 29 118 30 148
## 30 484 49 533
## 31 664 81 745
## 32 1004 125 1129
## 33 1231 142 1373
## 34 1372 174 1546
## 35 1582 177 1759
NewOrange %>% print(n=35)
## # A tibble: 35 × 3
## age circumference characteristic
## <dbl> <dbl> <dbl>
## 1 118 30 148
## 2 484 58 542
## 3 664 87 751
## 4 1004 115 1119
## 5 1231 120 1351
## 6 1372 142 1514
## 7 1582 145 1727
## 8 118 33 151
## 9 484 69 553
## 10 664 111 775
## 11 1004 156 1160
## 12 1231 172 1403
## 13 1372 203 1575
## 14 1582 203 1785
## 15 118 30 148
## 16 484 51 535
## 17 664 75 739
## 18 1004 108 1112
## 19 1231 115 1346
## 20 1372 139 1511
## 21 1582 140 1722
## 22 118 32 150
## 23 484 62 546
## 24 664 112 776
## 25 1004 167 1171
## 26 1231 179 1410
## 27 1372 209 1581
## 28 1582 214 1796
## 29 118 30 148
## 30 484 49 533
## 31 664 81 745
## 32 1004 125 1129
## 33 1231 142 1373
## 34 1372 174 1546
## 35 1582 177 1759