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)
library(dplyr)
data(women)
women <- tibble::as.tibble(women)
## 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(women)
## [1] "tbl_df" "tbl" "data.frame"
view(women)
glimpse(women)
## Rows: 15
## Columns: 2
## $ height <dbl> 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72
## $ weight <dbl> 115, 117, 120, 123, 126, 129, 132, 135, 139, 142, 146, 150, 154…
head(women)
## # A tibble: 6 × 2
## height weight
## <dbl> <dbl>
## 1 58 115
## 2 59 117
## 3 60 120
## 4 61 123
## 5 62 126
## 6 63 129
#Mutate
women %>% mutate (BMI=height/weight^2)
## # A tibble: 15 × 3
## height weight BMI
## <dbl> <dbl> <dbl>
## 1 58 115 0.00439
## 2 59 117 0.00431
## 3 60 120 0.00417
## 4 61 123 0.00403
## 5 62 126 0.00391
## 6 63 129 0.00379
## 7 64 132 0.00367
## 8 65 135 0.00357
## 9 66 139 0.00342
## 10 67 142 0.00332
## 11 68 146 0.00319
## 12 69 150 0.00307
## 13 70 154 0.00295
## 14 71 159 0.00281
## 15 72 164 0.00268
womenbaru <- women %>% mutate (BMI=height/weight^2) %>% select(weight, height, BMI)
womenbaru
## # A tibble: 15 × 3
## weight height BMI
## <dbl> <dbl> <dbl>
## 1 115 58 0.00439
## 2 117 59 0.00431
## 3 120 60 0.00417
## 4 123 61 0.00403
## 5 126 62 0.00391
## 6 129 63 0.00379
## 7 132 64 0.00367
## 8 135 65 0.00357
## 9 139 66 0.00342
## 10 142 67 0.00332
## 11 146 68 0.00319
## 12 150 69 0.00307
## 13 154 70 0.00295
## 14 159 71 0.00281
## 15 164 72 0.00268
#Rata-rata
mean(women$height)
## [1] 65
mean(women$height) == women$height %>% mean ()
## [1] TRUE
#Summarize & Arrange
## Menghitung rata-rata Women Height (Cm)
women %>% summarize(mean=mean(height))
## # A tibble: 1 × 1
## mean
## <dbl>
## 1 65
## Mengurutkan berdasarkan peubah Height dari nilai terbesar
women %>% arrange(desc(height)) %>% print(n=30)
## # A tibble: 15 × 2
## height weight
## <dbl> <dbl>
## 1 72 164
## 2 71 159
## 3 70 154
## 4 69 150
## 5 68 146
## 6 67 142
## 7 66 139
## 8 65 135
## 9 64 132
## 10 63 129
## 11 62 126
## 12 61 123
## 13 60 120
## 14 59 117
## 15 58 115
## Mengurutkan berdasarkan peubah Age dari nilai terkecil
women %>% arrange(height) %>% print(n=30)
## # A tibble: 15 × 2
## height weight
## <dbl> <dbl>
## 1 58 115
## 2 59 117
## 3 60 120
## 4 61 123
## 5 62 126
## 6 63 129
## 7 64 132
## 8 65 135
## 9 66 139
## 10 67 142
## 11 68 146
## 12 69 150
## 13 70 154
## 14 71 159
## 15 72 164
#Filter & Select
womenbaru %>% filter(height>=65)
## # A tibble: 8 × 3
## weight height BMI
## <dbl> <dbl> <dbl>
## 1 135 65 0.00357
## 2 139 66 0.00342
## 3 142 67 0.00332
## 4 146 68 0.00319
## 5 150 69 0.00307
## 6 154 70 0.00295
## 7 159 71 0.00281
## 8 164 72 0.00268
womenbaru %>% select(height, BMI)
## # A tibble: 15 × 2
## height BMI
## <dbl> <dbl>
## 1 58 0.00439
## 2 59 0.00431
## 3 60 0.00417
## 4 61 0.00403
## 5 62 0.00391
## 6 63 0.00379
## 7 64 0.00367
## 8 65 0.00357
## 9 66 0.00342
## 10 67 0.00332
## 11 68 0.00319
## 12 69 0.00307
## 13 70 0.00295
## 14 71 0.00281
## 15 72 0.00268
womenbaru %>% select(-height)
## # A tibble: 15 × 2
## weight BMI
## <dbl> <dbl>
## 1 115 0.00439
## 2 117 0.00431
## 3 120 0.00417
## 4 123 0.00403
## 5 126 0.00391
## 6 129 0.00379
## 7 132 0.00367
## 8 135 0.00357
## 9 139 0.00342
## 10 142 0.00332
## 11 146 0.00319
## 12 150 0.00307
## 13 154 0.00295
## 14 159 0.00281
## 15 164 0.00268