#Tugas
#Mengambil datasets
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)
library(dplyr)
#Memakai data quakes
data(quakes)
quakes <- tibble::as.tibble(quakes)
## 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(quakes)
## [1] "tbl_df" "tbl" "data.frame"
view(quakes)
head(quakes)
## # A tibble: 6 × 5
## lat long depth mag stations
## <dbl> <dbl> <int> <dbl> <int>
## 1 -20.4 182. 562 4.8 41
## 2 -20.6 181. 650 4.2 15
## 3 -26 184. 42 5.4 43
## 4 -18.0 182. 626 4.1 19
## 5 -20.4 182. 649 4 11
## 6 -19.7 184. 195 4 12
#Menggunakan fungsi summarize,arrange,select,filter,mutate
quakes %>% filter(mag=="4.4")
## # A tibble: 101 × 5
## lat long depth mag stations
## <dbl> <dbl> <int> <dbl> <int>
## 1 -28.1 182. 194 4.4 15
## 2 -21.4 181. 583 4.4 13
## 3 -18.5 182. 554 4.4 19
## 4 -21 182. 600 4.4 10
## 5 -23.5 180. 570 4.4 13
## 6 -22.6 180. 598 4.4 18
## 7 -23.3 180. 512 4.4 18
## 8 -19.8 182. 328 4.4 17
## 9 -18.8 169. 230 4.4 11
## 10 -25.0 180. 511 4.4 23
## # ℹ 91 more rows
quakes %>% select(lat,long,depth)
## # A tibble: 1,000 × 3
## lat long depth
## <dbl> <dbl> <int>
## 1 -20.4 182. 562
## 2 -20.6 181. 650
## 3 -26 184. 42
## 4 -18.0 182. 626
## 5 -20.4 182. 649
## 6 -19.7 184. 195
## 7 -11.7 166. 82
## 8 -28.1 182. 194
## 9 -28.7 182. 211
## 10 -17.5 180. 622
## # ℹ 990 more rows
quakes %>% select(-lat,-long)
## # A tibble: 1,000 × 3
## depth mag stations
## <int> <dbl> <int>
## 1 562 4.8 41
## 2 650 4.2 15
## 3 42 5.4 43
## 4 626 4.1 19
## 5 649 4 11
## 6 195 4 12
## 7 82 4.8 43
## 8 194 4.4 15
## 9 211 4.7 35
## 10 622 4.3 19
## # ℹ 990 more rows
quakes %>% group_by(stations) %>% summarize(mean=mean(mag))
## # A tibble: 102 × 2
## stations mean
## <int> <dbl>
## 1 10 4.23
## 2 11 4.23
## 3 12 4.20
## 4 13 4.33
## 5 14 4.28
## 6 15 4.28
## 7 16 4.27
## 8 17 4.35
## 9 18 4.44
## 10 19 4.38
## # ℹ 92 more rows
quakes %>% arrange(mag)
## # A tibble: 1,000 × 5
## lat long depth mag stations
## <dbl> <dbl> <int> <dbl> <int>
## 1 -20.4 182. 649 4 11
## 2 -19.7 184. 195 4 12
## 3 -17.9 181. 537 4 15
## 4 -23.6 181. 349 4 10
## 5 -19.3 184. 223 4 15
## 6 -22.1 181. 584 4 11
## 7 -15.3 186. 152 4 11
## 8 -17.7 182. 450 4 11
## 9 -19.7 182. 375 4 18
## 10 -19.1 182. 477 4 16
## # ℹ 990 more rows
quakes %>% arrange(desc(mag))
## # A tibble: 1,000 × 5
## lat long depth mag stations
## <dbl> <dbl> <int> <dbl> <int>
## 1 -15.6 168. 127 6.4 122
## 2 -20.7 170. 139 6.1 94
## 3 -13.6 166. 50 6 83
## 4 -12.2 167. 242 6 132
## 5 -21.6 171. 165 6 119
## 6 -22.9 184. 64 5.9 118
## 7 -21.1 181. 627 5.9 119
## 8 -22.6 186. 42 5.7 76
## 9 -23.3 184. 56 5.7 106
## 10 -32.2 180. 216 5.7 90
## # ℹ 990 more rows
quakes %>% mutate(longlat=long+lat)
## # A tibble: 1,000 × 6
## lat long depth mag stations longlat
## <dbl> <dbl> <int> <dbl> <int> <dbl>
## 1 -20.4 182. 562 4.8 41 161.
## 2 -20.6 181. 650 4.2 15 160.
## 3 -26 184. 42 5.4 43 158.
## 4 -18.0 182. 626 4.1 19 164.
## 5 -20.4 182. 649 4 11 162.
## 6 -19.7 184. 195 4 12 165.
## 7 -11.7 166. 82 4.8 43 154.
## 8 -28.1 182. 194 4.4 15 154.
## 9 -28.7 182. 211 4.7 35 153
## 10 -17.5 180. 622 4.3 19 162.
## # ℹ 990 more rows
quakesbaru <- quakes %>% select(-lat,-long) %>% mutate(jumlah=mag+stations)
quakesbaru
## # A tibble: 1,000 × 4
## depth mag stations jumlah
## <int> <dbl> <int> <dbl>
## 1 562 4.8 41 45.8
## 2 650 4.2 15 19.2
## 3 42 5.4 43 48.4
## 4 626 4.1 19 23.1
## 5 649 4 11 15
## 6 195 4 12 16
## 7 82 4.8 43 47.8
## 8 194 4.4 15 19.4
## 9 211 4.7 35 39.7
## 10 622 4.3 19 23.3
## # ℹ 990 more rows