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)
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
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 %>% filter(mag=="4.8")
## # A tibble: 65 × 5
## lat long depth mag stations
## <dbl> <dbl> <int> <dbl> <int>
## 1 -20.4 182. 562 4.8 41
## 2 -11.7 166. 82 4.8 43
## 3 -15.6 185. 292 4.8 42
## 4 -26.1 178. 617 4.8 39
## 5 -14.6 167. 178 4.8 52
## 6 -14.6 167. 82 4.8 28
## 7 -27.7 182. 94 4.8 59
## 8 -24.3 180. 504 4.8 34
## 9 -35.5 180. 59 4.8 35
## 10 -25.0 180. 470 4.8 41
## # ℹ 55 more rows
quakes %>% select(long, depth, mag)
## # A tibble: 1,000 × 3
## long depth mag
## <dbl> <int> <dbl>
## 1 182. 562 4.8
## 2 181. 650 4.2
## 3 184. 42 5.4
## 4 182. 626 4.1
## 5 182. 649 4
## 6 184. 195 4
## 7 166. 82 4.8
## 8 182. 194 4.4
## 9 182. 211 4.7
## 10 180. 622 4.3
## # ℹ 990 more rows
quakes %>% select(-depth,-mag)
## # A tibble: 1,000 × 3
## lat long stations
## <dbl> <dbl> <int>
## 1 -20.4 182. 41
## 2 -20.6 181. 15
## 3 -26 184. 43
## 4 -18.0 182. 19
## 5 -20.4 182. 11
## 6 -19.7 184. 12
## 7 -11.7 166. 43
## 8 -28.1 182. 15
## 9 -28.7 182. 35
## 10 -17.5 180. 19
## # ℹ 990 more rows
quakes %>% mutate(longdepth=long+depth)
## # A tibble: 1,000 × 6
## lat long depth mag stations longdepth
## <dbl> <dbl> <int> <dbl> <int> <dbl>
## 1 -20.4 182. 562 4.8 41 744.
## 2 -20.6 181. 650 4.2 15 831.
## 3 -26 184. 42 5.4 43 226.
## 4 -18.0 182. 626 4.1 19 808.
## 5 -20.4 182. 649 4 11 831.
## 6 -19.7 184. 195 4 12 379.
## 7 -11.7 166. 82 4.8 43 248.
## 8 -28.1 182. 194 4.4 15 376.
## 9 -28.7 182. 211 4.7 35 393.
## 10 -17.5 180. 622 4.3 19 802.
## # ℹ 990 more rows
quakesbaru <- quakes %>% select(-long,-depth) %>% mutate(jumlah=mag+stations)
quakesbaru
## # A tibble: 1,000 × 4
## lat mag stations jumlah
## <dbl> <dbl> <int> <dbl>
## 1 -20.4 4.8 41 45.8
## 2 -20.6 4.2 15 19.2
## 3 -26 5.4 43 48.4
## 4 -18.0 4.1 19 23.1
## 5 -20.4 4 11 15
## 6 -19.7 4 12 16
## 7 -11.7 4.8 43 47.8
## 8 -28.1 4.4 15 19.4
## 9 -28.7 4.7 35 39.7
## 10 -17.5 4.3 19 23.3
## # ℹ 990 more rows