title: “Week 2 HW” author: “Bryan Persaud” date: “7/28/2019” output: html_document
theUrl <- "https://raw.githubusercontent.com/bpersaud104/R/master/quakes.csv"
quakes <- read.table(file = theUrl, header = TRUE, sep = ",")
head(quakes, 50)
## X lat long depth mag stations
## 1 1 -20.42 181.62 562 4.8 41
## 2 2 -20.62 181.03 650 4.2 15
## 3 3 -26.00 184.10 42 5.4 43
## 4 4 -17.97 181.66 626 4.1 19
## 5 5 -20.42 181.96 649 4.0 11
## 6 6 -19.68 184.31 195 4.0 12
## 7 7 -11.70 166.10 82 4.8 43
## 8 8 -28.11 181.93 194 4.4 15
## 9 9 -28.74 181.74 211 4.7 35
## 10 10 -17.47 179.59 622 4.3 19
## 11 11 -21.44 180.69 583 4.4 13
## 12 12 -12.26 167.00 249 4.6 16
## 13 13 -18.54 182.11 554 4.4 19
## 14 14 -21.00 181.66 600 4.4 10
## 15 15 -20.70 169.92 139 6.1 94
## 16 16 -15.94 184.95 306 4.3 11
## 17 17 -13.64 165.96 50 6.0 83
## 18 18 -17.83 181.50 590 4.5 21
## 19 19 -23.50 179.78 570 4.4 13
## 20 20 -22.63 180.31 598 4.4 18
## 21 21 -20.84 181.16 576 4.5 17
## 22 22 -10.98 166.32 211 4.2 12
## 23 23 -23.30 180.16 512 4.4 18
## 24 24 -30.20 182.00 125 4.7 22
## 25 25 -19.66 180.28 431 5.4 57
## 26 26 -17.94 181.49 537 4.0 15
## 27 27 -14.72 167.51 155 4.6 18
## 28 28 -16.46 180.79 498 5.2 79
## 29 29 -20.97 181.47 582 4.5 25
## 30 30 -19.84 182.37 328 4.4 17
## 31 31 -22.58 179.24 553 4.6 21
## 32 32 -16.32 166.74 50 4.7 30
## 33 33 -15.55 185.05 292 4.8 42
## 34 34 -23.55 180.80 349 4.0 10
## 35 35 -16.30 186.00 48 4.5 10
## 36 36 -25.82 179.33 600 4.3 13
## 37 37 -18.73 169.23 206 4.5 17
## 38 38 -17.64 181.28 574 4.6 17
## 39 39 -17.66 181.40 585 4.1 17
## 40 40 -18.82 169.33 230 4.4 11
## 41 41 -37.37 176.78 263 4.7 34
## 42 42 -15.31 186.10 96 4.6 32
## 43 43 -24.97 179.82 511 4.4 23
## 44 44 -15.49 186.04 94 4.3 26
## 45 45 -19.23 169.41 246 4.6 27
## 46 46 -30.10 182.30 56 4.9 34
## 47 47 -26.40 181.70 329 4.5 24
## 48 48 -11.77 166.32 70 4.4 18
## 49 49 -24.12 180.08 493 4.3 21
## 50 50 -18.97 185.25 129 5.1 73
summary(quakes)
## X lat long depth
## Min. : 1.0 Min. :-38.59 Min. :165.7 Min. : 40.0
## 1st Qu.: 250.8 1st Qu.:-23.47 1st Qu.:179.6 1st Qu.: 99.0
## Median : 500.5 Median :-20.30 Median :181.4 Median :247.0
## Mean : 500.5 Mean :-20.64 Mean :179.5 Mean :311.4
## 3rd Qu.: 750.2 3rd Qu.:-17.64 3rd Qu.:183.2 3rd Qu.:543.0
## Max. :1000.0 Max. :-10.72 Max. :188.1 Max. :680.0
## mag stations
## Min. :4.00 Min. : 10.00
## 1st Qu.:4.30 1st Qu.: 18.00
## Median :4.60 Median : 27.00
## Mean :4.62 Mean : 33.42
## 3rd Qu.:4.90 3rd Qu.: 42.00
## Max. :6.40 Max. :132.00
# The two attributes I am using are "depth" and "mag"
# Mean and median for "depth"
depth_mean <- mean(quakes$depth)
depth_median <- median(quakes$depth)
print(paste("The mean of depth is = ", round(depth_mean, 2), "and the median of depth = ", depth_median))
## [1] "The mean of depth is = 311.37 and the median of depth = 247"
# Mean and median for "mag"
mag_mean <- mean(quakes$mag)
mag_median <- median(quakes$mag)
print(paste("The mean of mag is = ", round(mag_mean, 2), "and the median of mag = ", mag_median))
## [1] "The mean of mag is = 4.62 and the median of mag = 4.6"
sub_quakes <- data.frame(subset(quakes, depth > 350 & mag >= 4.5))
names(sub_quakes) <- c("X" = "Y", "lat" = "nlat", "long" = "nlong", "depth" = "ndepth", "mag" = "nmag", "stations" = "nstations") # n = new
summary(sub_quakes)
## Y nlat nlong ndepth
## Min. : 1.0 Min. :-32.20 Min. :169.1 Min. :361.0
## 1st Qu.:261.0 1st Qu.:-23.50 1st Qu.:180.0 1st Qu.:511.0
## Median :463.0 Median :-21.08 Median :180.9 Median :561.0
## Mean :478.5 Mean :-21.13 Mean :180.4 Mean :549.7
## 3rd Qu.:693.0 3rd Qu.:-18.48 3rd Qu.:181.5 3rd Qu.:595.0
## Max. :981.0 Max. :-12.66 Max. :183.6 Max. :664.0
## nmag nstations
## Min. :4.500 Min. : 10.00
## 1st Qu.:4.600 1st Qu.: 27.00
## Median :4.700 Median : 39.00
## Mean :4.808 Mean : 43.81
## 3rd Qu.:5.000 3rd Qu.: 56.00
## Max. :5.900 Max. :129.00
# Mean and median for the new "depth"
ndepth_mean <- mean(sub_quakes$ndepth)
ndepth_median <- median(sub_quakes$ndepth)
print(paste("The mean of the new depth = ", round(ndepth_mean, 2), "and the median of the new depth = ", ndepth_median))
## [1] "The mean of the new depth = 549.74 and the median of the new depth = 561"
# Compare to Question 1's depth mean and median
print(paste("The mean for depth = ", round(depth_mean, 2), "and the mean for the new depth = ", round(ndepth_mean, 2)))
## [1] "The mean for depth = 311.37 and the mean for the new depth = 549.74"
print(paste("The median for depth = ", depth_median, "and the median for the new depth = ", ndepth_median))
## [1] "The median for depth = 247 and the median for the new depth = 561"
# Mean and median for the new "mag"
nmag_mean <- mean(sub_quakes$nmag)
nmag_median <- median(sub_quakes$nmag)
print(paste("The mean for the new mag = ", round(nmag_mean, 2), "and the median for the new mag = ", nmag_median))
## [1] "The mean for the new mag = 4.81 and the median for the new mag = 4.7"
# Compare to Question 1's mag mean and median
print(paste("The mean for mag = ", round(mag_mean, 2), "and the mean for the new mag = ", round(nmag_mean, 2)))
## [1] "The mean for mag = 4.62 and the mean for the new mag = 4.81"
print(paste("The medain for mag = ", mag_median, "and the median for the new mag = ", nmag_median))
## [1] "The medain for mag = 4.6 and the median for the new mag = 4.7"
sub_quakes$nmag[sub_quakes$nmag == 4.6] <- "light"
sub_quakes$nmag[sub_quakes$nmag == 5.1] <- "moderate"
sub_quakes$nmag[sub_quakes$nmag == 5.7] <- "strong"
head(sub_quakes, 100)
## Y nlat nlong ndepth nmag nstations
## 1 1 -20.42 181.62 562 4.8 41
## 18 18 -17.83 181.50 590 4.5 21
## 21 21 -20.84 181.16 576 4.5 17
## 25 25 -19.66 180.28 431 5.4 57
## 28 28 -16.46 180.79 498 5.2 79
## 29 29 -20.97 181.47 582 4.5 25
## 31 31 -22.58 179.24 553 light 21
## 38 38 -17.64 181.28 574 light 17
## 57 57 -22.70 181.00 445 4.5 17
## 61 61 -20.69 181.55 582 4.7 35
## 63 63 -13.82 172.38 613 5 61
## 65 65 -20.68 181.41 593 4.9 40
## 68 68 -21.96 179.62 627 5 45
## 69 69 -20.42 181.86 530 4.5 27
## 74 74 -23.74 179.99 506 5.2 75
## 82 82 -23.84 180.99 367 4.5 27
## 83 83 -19.57 182.38 579 light 38
## 88 88 -23.64 179.96 538 4.5 26
## 93 93 -20.64 182.02 497 5.2 64
## 97 97 -27.24 181.11 365 4.5 21
## 100 100 -24.57 179.92 484 4.7 33
## 102 102 -26.20 178.41 583 light 25
## 103 103 -21.88 180.39 608 4.7 30
## 105 105 -21.33 180.69 636 light 29
## 114 114 -26.11 178.30 617 4.8 39
## 123 123 -22.05 180.40 606 4.7 27
## 124 124 -19.22 182.43 571 4.5 23
## 128 128 -26.53 178.57 600 5 69
## 134 134 -23.71 180.30 510 light 30
## 141 141 -12.66 169.46 658 light 43
## 149 149 -23.58 180.17 462 5.3 63
## 156 156 -20.65 181.32 597 4.7 39
## 162 162 -24.34 179.52 504 4.8 34
## 169 169 -23.43 180.00 553 4.7 41
## 172 172 -25.68 180.34 434 light 41
## 177 177 -22.64 180.64 544 5 50
## 181 181 -18.04 181.75 640 4.5 47
## 188 188 -19.85 181.85 576 4.9 54
## 189 189 -24.27 179.88 523 light 24
## 193 193 -17.87 182.00 569 light 12
## 195 195 -32.20 179.61 422 light 41
## 200 200 -17.72 180.30 595 5.2 74
## 203 203 -16.23 183.59 367 4.7 35
## 205 205 -12.95 169.09 629 4.5 19
## 207 207 -21.96 180.54 603 5.2 66
## 208 208 -20.32 181.69 508 4.5 14
## 211 211 -30.66 180.13 411 4.7 42
## 218 218 -18.13 181.52 618 light 41
## 225 225 -23.77 180.16 505 4.5 26
## 231 231 -19.40 180.94 664 4.7 34
## 235 235 -23.84 180.13 525 4.5 15
## 238 238 -21.68 180.63 617 5 63
## 240 240 -24.96 180.22 470 4.8 41
## 246 246 -18.11 181.67 597 light 28
## 249 249 -23.36 180.01 553 5.3 61
## 253 253 -17.80 181.38 587 moderate 47
## 260 260 -23.79 179.89 526 4.9 43
## 262 262 -20.90 181.51 548 4.7 32
## 269 269 -20.21 182.37 482 light 37
## 270 270 -21.29 180.85 607 4.5 23
## 272 272 -22.09 180.38 590 4.9 35
## 275 275 -22.13 180.38 577 strong 104
## 276 276 -17.44 181.40 529 light 25
## 277 277 -23.33 180.18 528 5 59
## 279 279 -22.00 180.52 561 4.5 19
## 280 280 -19.13 182.51 579 5.2 56
## 286 286 -24.40 179.85 522 4.7 29
## 289 289 -18.07 181.57 572 4.5 26
## 290 290 -20.60 182.28 529 5 50
## 291 291 -18.48 181.49 641 5 49
## 293 293 -20.92 181.50 546 light 31
## 294 294 -25.31 179.69 507 light 35
## 297 297 -24.57 178.40 562 5.6 80
## 304 304 -21.09 181.38 555 light 15
## 306 306 -23.30 179.70 500 4.7 29
## 308 308 -22.00 180.53 583 4.9 20
## 309 309 -21.38 181.39 501 light 36
## 311 311 -13.05 169.58 644 4.9 68
## 312 312 -12.93 169.63 641 moderate 57
## 313 313 -18.60 181.91 442 5.4 82
## 314 314 -21.34 181.41 464 4.5 21
## 319 319 -26.16 179.50 492 4.5 25
## 332 332 -23.91 180.00 534 4.5 11
## 335 335 -23.49 179.07 544 moderate 58
## 356 356 -17.79 181.32 587 5 49
## 358 358 -17.10 182.68 403 5.5 82
## 363 363 -21.98 179.60 583 5.4 67
## 367 367 -20.43 182.37 502 moderate 48
## 371 371 -23.73 179.99 527 moderate 49
## 373 373 -17.59 181.09 536 moderate 61
## 374 374 -19.77 181.40 630 moderate 54
## 385 385 -20.04 182.01 605 moderate 49
## 388 388 -27.23 180.98 401 4.5 39
## 395 395 -21.04 181.20 591 4.9 45
## 397 397 -17.72 181.42 565 5.3 89
## 399 399 -17.84 181.30 535 strong 112
## 400 400 -13.45 170.30 641 5.3 93
## 404 404 -26.18 178.59 548 5.4 65
## 424 424 -22.10 179.71 579 moderate 58
## 427 427 -20.58 181.24 602 4.7 44