# Merissah Gilbert
# 1. I did this homework by myself, with help from the book and the professor.
Assignment arrow <-
The combine command c( )
Descriptive
statistics mean( ) sum( ) max( )
Arithmetic operators + - * /
Boolean operators > < >= <= == !=
This Week: Explore the quakes dataset (which is included in R). Copy the quakes dataset into a new dataframe (call it myQuakes), so that if you need to start over, you can do so easily (by copying quakes into myQuakes again). Summarize the variables in myQuakes. Also explore the structure of the dataframe
myQuakes <- quakes
str(myQuakes)
## 'data.frame': 1000 obs. of 5 variables:
## $ lat : num -20.4 -20.6 -26 -18 -20.4 ...
## $ long : num 182 181 184 182 182 ...
## $ depth : int 562 650 42 626 649 195 82 194 211 622 ...
## $ mag : num 4.8 4.2 5.4 4.1 4 4 4.8 4.4 4.7 4.3 ...
## $ stations: int 41 15 43 19 11 12 43 15 35 19 ...
Step 1: Explore the earthquake magnitude variable
called mag
mean(myQuakes$mag)
## [1] 4.6204
maxQuake <- max(myQuakes$mag)
minQuake <- min(myQuakes$mag)
myQuakes[3,]
## lat long depth mag stations
## 3 -26 184.1 42 5.4 43
E. Create a new dataframe, with only the rows where the magnitude is greater than 4. How many rows are in that dataframe (use code, do not count by looking at the output)
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.4 ✔ readr 2.1.5
## ✔ forcats 1.0.0 ✔ stringr 1.5.1
## ✔ ggplot2 3.5.1 ✔ tibble 3.2.1
## ✔ lubridate 1.9.3 ✔ tidyr 1.3.1
## ✔ 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
mag <- myQuakes$mag
df <- filter(myQuakes, mag >4)
df <- tibble::rowid_to_column(df, "index")
tail(df)
## index lat long depth mag stations
## 949 949 -17.70 188.10 45 4.2 10
## 950 950 -25.93 179.54 470 4.4 22
## 951 951 -12.28 167.06 248 4.7 35
## 952 952 -20.13 184.20 244 4.5 34
## 953 953 -17.40 187.80 40 4.5 14
## 954 954 -21.59 170.56 165 6.0 119
library(tidyverse)
quakeSorted1 <- arrange(myQuakes, mag)
quakeSorted1 <- myQuakes[order(myQuakes$mag),]
view(myQuakes)
max(myQuakes$stations)
## [1] 132
myQuakes %>%
slice_max(stations)
## lat long depth mag stations
## 1 -12.23 167.02 242 6 132
myQuakes %>%
slice_min(stations)
## lat long depth mag stations
## 1 -21.00 181.66 600 4.4 10
## 2 -23.55 180.80 349 4.0 10
## 3 -16.30 186.00 48 4.5 10
## 4 -20.10 184.40 186 4.2 10
## 5 -15.03 182.29 399 4.1 10
## 6 -19.06 169.01 158 4.4 10
## 7 -17.70 185.00 383 4.0 10
## 8 -21.04 181.20 483 4.2 10
## 9 -27.21 182.43 55 4.6 10
## 10 -18.40 183.40 343 4.1 10
## 11 -20.30 182.30 476 4.5 10
## 12 -14.85 184.87 294 4.1 10
## 13 -17.60 181.50 548 4.1 10
## 14 -20.61 182.44 518 4.2 10
## 15 -25.00 180.00 488 4.5 10
## 16 -17.78 185.33 223 4.1 10
## 17 -20.70 186.30 80 4.0 10
## 18 -21.77 181.00 618 4.1 10
## 19 -21.05 180.90 616 4.3 10
## 20 -17.70 188.10 45 4.2 10
Step 3: Using conditional if statements
if (maxQuake > 7) {print("yes")} else {print("no")}
## [1] "no"
if (minQuake < 3) {print("yes")} else {print("no")}
## [1] "no"