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)

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
glimpse(quakes) 
## Rows: 1,000
## Columns: 5
## $ lat      <dbl> -20.42, -20.62, -26.00, -17.97, -20.42, -19.68, -11.70, -28.1…
## $ long     <dbl> 181.62, 181.03, 184.10, 181.66, 181.96, 184.31, 166.10, 181.9…
## $ depth    <int> 562, 650, 42, 626, 649, 195, 82, 194, 211, 622, 583, 249, 554…
## $ mag      <dbl> 4.8, 4.2, 5.4, 4.1, 4.0, 4.0, 4.8, 4.4, 4.7, 4.3, 4.4, 4.6, 4…
## $ stations <int> 41, 15, 43, 19, 11, 12, 43, 15, 35, 19, 13, 16, 19, 10, 94, 1…
#SUMMARIZE
# Menghitung rata-rata long setiap stations
quakes %>%  group_by(stations) %>% summarize(mean=mean(long))
## # A tibble: 102 × 2
##    stations  mean
##       <int> <dbl>
##  1       10  182.
##  2       11  182.
##  3       12  180.
##  4       13  181.
##  5       14  179.
##  6       15  181.
##  7       16  179.
##  8       17  181.
##  9       18  179.
## 10       19  181.
## # ℹ 92 more rows
## Mengurutkan berdasarkan peubah mag dari nilai terkecil
quakes %>% arrange(mag) %>%  print (n=100)
## # 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
##  11 -20.6  181.   582   4         14
##  12 -17.9  182.   573   4         19
##  13 -17.7  182.   445   4         12
##  14 -23.5  180.   574   4         12
##  15 -17.7  185    383   4         10
##  16 -17.9  182.   601   4         16
##  17 -30.6  181.   175   4         16
##  18 -16.9  186.   135   4         22
##  19 -10.7  166.   195   4         14
##  20 -18.6  182.   563   4         17
##  21 -22.7  183.   180   4         13
##  22 -21    183.   296   4         16
##  23 -17.0  183.   406   4         17
##  24 -19.5  184.   280   4         16
##  25 -15.4  185.   249   4         11
##  26 -17.9  181.   555   4         17
##  27 -17.1  183.   390   4         14
##  28 -30.3  181.   275   4         14
##  29 -25.6  180.   440   4         12
##  30 -20.7  186.    80   4         10
##  31 -16.4  183.   391   4         16
##  32 -21.6  180.   595   4         22
##  33 -17.9  182.   589   4         12
##  34 -28    182    199   4         16
##  35 -22.1  180.   532   4         14
##  36 -21.6  182.   350   4         12
##  37 -19.7  182.   397   4         12
##  38 -18.5  185.   243   4         11
##  39 -21.0  181.   638   4         14
##  40 -17.8  181.   530   4         15
##  41 -17.8  181.   538   4         33
##  42 -25.1  183.   133   4         14
##  43 -18.1  182.   574   4         20
##  44 -23.5  180.   530   4         23
##  45 -17.9  181.   614   4         12
##  46 -18.0  181.   642   4         17
##  47 -18.0  182.   626   4.1       19
##  48 -17.7  181.   585   4.1       17
##  49 -21.2  182.   260   4.1       12
##  50 -20.1  182.   587   4.1       13
##  51 -19.2  185.   197   4.1       11
##  52 -17.0  186.   108   4.1       12
##  53 -18.0  180.   626   4.1       19
##  54 -20.2  181.   627   4.1       11
##  55 -15.2  185.    99   4.1       14
##  56 -15.0  182.   399   4.1       10
##  57 -17.8  181.   542   4.1       20
##  58 -20.4  182.   534   4.1       14
##  59 -24.1  180.   605   4.1       21
##  60 -28.2  182.   226   4.1       19
##  61 -24.4  183.   148   4.1       16
##  62 -23.8  180.   498   4.1       12
##  63 -17.7  181.   515   4.1       19
##  64 -17.3  181.   497   4.1       13
##  65 -17.5  181.   573   4.1       17
##  66 -23.4  183.   158   4.1       20
##  67 -17.0  187.    70   4.1       22
##  68 -18.4  183.   343   4.1       10
##  69 -14.8  185.   294   4.1       10
##  70 -17.9  181.   593   4.1       13
##  71 -17.6  181.   537   4.1       11
##  72 -17.1  185.   223   4.1       15
##  73 -17.8  182.   598   4.1       14
##  74 -19.2  183.   570   4.1       22
##  75 -21.8  185     74   4.1       15
##  76 -17.8  181.   539   4.1       12
##  77 -18.9  181.   655   4.1       14
##  78 -17.6  182.   548   4.1       10
##  79 -17.2  183.   383   4.1       11
##  80 -25.1  178.   554   4.1       15
##  81 -16.4  187.    75   4.1       20
##  82 -17.8  185.   223   4.1       10
##  83 -19.1  185.   230   4.1       16
##  84 -21.8  181    618   4.1       10
##  85 -18.1  182.   593   4.1       16
##  86 -20.2  182.   576   4.1       16
##  87 -19.1  185.   210   4.1       22
##  88 -22.1  180.   603   4.1       11
##  89 -22.1  180.   587   4.1       23
##  90 -15.7  185.    70   4.1       15
##  91 -19.4  182.   326   4.1       15
##  92 -15.8  186.   121   4.1       17
##  93 -17.6  181.   580   4.1       16
##  94 -15.2  186.    77   4.1       16
##  95 -15.6  185.   315   4.1       15
##  96 -17.9  182.   567   4.1       27
##  97 -17.7  181.   559   4.1       16
##  98 -18.4  182.   600   4.1       11
##  99 -23.5  180.   543   4.1       21
## 100 -19.7  184.   223   4.1       23
## # ℹ 900 more rows
## Mengurutkan berdasarkan peubah Sepal.Length dari nilai terbesar
quakes %>% arrange(desc(mag)) %>% print(n=100)
## # 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
##  11 -22.1  180.   577   5.7      104
##  12 -15.3  187.    48   5.7      123
##  13 -17.8  181.   535   5.7      112
##  14 -19.9  174.   546   5.7       99
##  15 -21.1  174.    40   5.7       78
##  16 -26    182.   205   5.6       98
##  17 -24.6  178.   562   5.6       80
##  18 -34.7  180.    75   5.6       79
##  19 -18.8  182.   417   5.6      129
##  20 -37.0  178.   153   5.6       87
##  21 -11.4  166.    93   5.6       94
##  22 -15.9  168.   183   5.6      109
##  23 -17.8  181.   589   5.6      115
##  24 -20.2  185.   107   5.6      121
##  25 -15.5  188.    40   5.5       91
##  26 -30.2  182.    56   5.5       68
##  27 -17.1  183.   403   5.5       82
##  28 -15.4  187.   130   5.5       95
##  29 -21.1  182.   538   5.5      104
##  30 -22.5  173.    54   5.5       71
##  31 -20.9  184.    58   5.5       92
##  32 -32.4  181.    41   5.5       81
##  33 -15.8  167.    64   5.5       73
##  34 -15.9  167.    40   5.5       86
##  35 -18.1  181.   624   5.5      105
##  36 -13.8  167.    42   5.5       70
##  37 -27.9  183.    87   5.5       67
##  38 -35.9  179.   138   5.5       78
##  39 -26    184.    42   5.4       43
##  40 -19.7  180.   431   5.4       57
##  41 -20.5  186.    93   5.4       85
##  42 -18.6  182.   442   5.4       82
##  43 -27.2  182.    69   5.4       68
##  44 -22.0  180.   583   5.4       67
##  45 -19.9  184.   219   5.4      105
##  46 -26.2  179.   548   5.4       65
##  47 -23.5  180.   538   5.4       87
##  48 -37.9  177.    65   5.4       65
##  49 -16.0  167.    47   5.4       87
##  50 -11.5  166.    89   5.4       80
##  51 -18.1  182.   649   5.4       88
##  52 -17.1  186.   127   5.4       75
##  53 -38.3  177.   100   5.4       71
##  54 -29.3  183.    57   5.4       61
##  55 -19.3  186.    44   5.4      110
##  56 -23.6  184.   118   5.4       88
##  57 -22.0  184.   109   5.4       61
##  58 -24.0  185.    43   5.4       45
##  59 -29.0  181.   304   5.3       60
##  60 -13.7  172.    46   5.3       67
##  61 -23.6  180.   462   5.3       63
##  62 -19.9  184.   244   5.3       73
##  63 -20.0  184.   234   5.3       71
##  64 -14.7  166     48   5.3       16
##  65 -23.4  180.   553   5.3       61
##  66 -18.8  169.   218   5.3       53
##  67 -11.4  166.    83   5.3       55
##  68 -19.1  184.    61   5.3       42
##  69 -17.7  181.   565   5.3       89
##  70 -13.4  170.   641   5.3       93
##  71 -21.9  183.   273   5.3       78
##  72 -24.2  179.   550   5.3       86
##  73 -15.3  167.   128   5.3       18
##  74 -18.9  169.   248   5.3       60
##  75 -21.3  186.    57   5.3       69
##  76 -21.1  186.    85   5.3       86
##  77 -13.7  167.    46   5.3       71
##  78 -14.1  167.    63   5.3       69
##  79 -16.0  168.   190   5.3       81
##  80 -16.5  181.   498   5.2       79
##  81 -23.7  180.   506   5.2       75
##  82 -34.0  180.    75   5.2       65
##  83 -20.6  182.   497   5.2       64
##  84 -18.2  183.   306   5.2       54
##  85 -17.7  180.   595   5.2       74
##  86 -22.0  181.   603   5.2       66
##  87 -19.1  183.   579   5.2       56
##  88 -21.8  183.   210   5.2       69
##  89 -18.1  181.   628   5.2       72
##  90 -23.6  183.    94   5.2       79
##  91 -17.7  181.   574   5.2       67
##  92 -14.5  167.   195   5.2       87
##  93 -18.5  183.   405   5.2       74
##  94 -28.0  182.    53   5.2       89
##  95 -18.2  181.   631   5.2       69
##  96 -18.8  182.   385   5.2       67
##  97 -17.6  177.   545   5.2       91
##  98 -18.0  182.   586   5.2       68
##  99 -19.0  187.    45   5.2       65
## 100 -31.0  182.    57   5.2       49
## # ℹ 900 more rows
#FILTER
quakes %>% filter(stations=="35")
## # A tibble: 20 × 5
##      lat  long depth   mag stations
##    <dbl> <dbl> <int> <dbl>    <int>
##  1 -28.7  182.   211   4.7       35
##  2 -20.7  182.   582   4.7       35
##  3 -17.7  181.   546   4.4       35
##  4 -37.0  178.   146   5         35
##  5 -35.5  180.    59   4.8       35
##  6 -18.6  169.   223   4.7       35
##  7 -16.2  184.   367   4.7       35
##  8 -19.2  169.   254   4.7       35
##  9 -22.1  180.   590   4.9       35
## 10 -25.3  180.   507   4.6       35
## 11 -17.7  187.   112   4.5       35
## 12 -33.6  181.    51   4.7       35
## 13 -18.8  188.    44   4.8       35
## 14 -12.8  167.   150   4.9       35
## 15 -22.8  171.    67   4.8       35
## 16 -21.1  181.   625   4.5       35
## 17 -19.6  170.   204   4.6       35
## 18 -23.7  184.    60   4.7       35
## 19 -18.6  169.   217   4.9       35
## 20 -12.3  167.   248   4.7       35
quakes %>% select(lat, depth, stations)
## # A tibble: 1,000 × 3
##      lat depth stations
##    <dbl> <int>    <int>
##  1 -20.4   562       41
##  2 -20.6   650       15
##  3 -26      42       43
##  4 -18.0   626       19
##  5 -20.4   649       11
##  6 -19.7   195       12
##  7 -11.7    82       43
##  8 -28.1   194       15
##  9 -28.7   211       35
## 10 -17.5   622       19
## # ℹ 990 more rows
quakes %>% select(-lat, -long, -depth)
## # A tibble: 1,000 × 2
##      mag stations
##    <dbl>    <int>
##  1   4.8       41
##  2   4.2       15
##  3   5.4       43
##  4   4.1       19
##  5   4         11
##  6   4         12
##  7   4.8       43
##  8   4.4       15
##  9   4.7       35
## 10   4.3       19
## # ℹ 990 more rows
#MUTATE
quakes %>% mutate(potential=depth/mag)
## # A tibble: 1,000 × 6
##      lat  long depth   mag stations potential
##    <dbl> <dbl> <int> <dbl>    <int>     <dbl>
##  1 -20.4  182.   562   4.8       41    117.  
##  2 -20.6  181.   650   4.2       15    155.  
##  3 -26    184.    42   5.4       43      7.78
##  4 -18.0  182.   626   4.1       19    153.  
##  5 -20.4  182.   649   4         11    162.  
##  6 -19.7  184.   195   4         12     48.8 
##  7 -11.7  166.    82   4.8       43     17.1 
##  8 -28.1  182.   194   4.4       15     44.1 
##  9 -28.7  182.   211   4.7       35     44.9 
## 10 -17.5  180.   622   4.3       19    145.  
## # ℹ 990 more rows
updatedquakes <- quakes %>%  select(-lat, -long) %>%  mutate(potential=depth/mag)
updatedquakes
## # A tibble: 1,000 × 4
##    depth   mag stations potential
##    <int> <dbl>    <int>     <dbl>
##  1   562   4.8       41    117.  
##  2   650   4.2       15    155.  
##  3    42   5.4       43      7.78
##  4   626   4.1       19    153.  
##  5   649   4         11    162.  
##  6   195   4         12     48.8 
##  7    82   4.8       43     17.1 
##  8   194   4.4       15     44.1 
##  9   211   4.7       35     44.9 
## 10   622   4.3       19    145.  
## # ℹ 990 more rows