This homework is due on Sunday, Jan 31 at 11:59 pm. Please submit as an HTML file on Canvas.
For all questions, include the R commands/functions that you used to find your answer. Answers without supporting code will not receive credit.
All homework assignments will be completed using R Markdown. These
.Rmdfiles consist of text/syntax (formatted using Markdown) alongside embedded R code. When you have completed the assignment (by adding R code inside codeblocks and supporting text outside codeblocks), create your document as follows:
- Click the “Knit” button (above)
- Fix any errors in your code, if applicable
- Upload the HTML file to Canvas
quakes contains information about earthquakes occurring near Fiji since 1964. The first few observations are listed below.head(quakes)
## lat long depth mag stations
## 1 -20.42 181.62 562 4.8 41
## 2 -20.62 181.03 650 4.2 15
## 3 -26.00 184.10 42 5.4 43
## 4 -17.97 181.66 626 4.1 19
## 5 -20.42 181.96 649 4.0 11
## 6 -19.68 184.31 195 4.0 12
NROW(quakes)
## [1] 1000
The data set give the locations of 1000 seismic events of MB > 4.0. The events occurred in a cube near Fiji since 1964. I used NROW command to list the observations in one random variable.
mag and depth? Note that there are many functions that can be used to answer this question. If you chose to work with each variable separately, recall that you can access individual variables in a dataframe using the $ operator (e.g., dataset$variable). Describe your answer in words.min(quakes$mag)
## [1] 4
max(quakes$mag)
## [1] 6.4
mean(quakes$mag)
## [1] 4.6204
median(quakes$mag)
## [1] 4.6
min(quakes$depth)
## [1] 40
max(quakes$depth)
## [1] 680
mean(quakes$depth)
## [1] 311.371
median(quakes$depth)
## [1] 247
The minimum value for mag is 4.The maximum value for mag is 6.4.The mean value for mag is 4.6204.The median value for mag is 4.6.The minimum value for depth is 40.The maximum value for depth is 680.The mean value for depth is 311.371.The median value for depth is 247.
median(quakes$mag[quakes$long>175])
## [1] 4.5
median(quakes[quakes$long>175,]$mag) #this is the more conventional notation
## [1] 4.5
$ selects a single variable and that [ ] are used for indexing whatever object came before (either a single variable or a dataframe).The first line first subset the longitude variable with values greater than 175 then it displays the median magnitudes of quakes from the longitude values. The second command subsets the longitude values between the quakes\(mag followed by a comma to specify the position where longitude subset is between quakes\)mag.
mag when depth is greater than the median depth? What is the mean of the variable mag when depth is less than the median depth? What does this suggest about the relationship between an earthquake’s depth and its magnitude?mean(quakes$mag[quakes$depth>median(quakes$depth)])
## [1] 4.5232
mean(quakes$mag[quakes$depth<median(quakes$depth)])
## [1] 4.7176
There is an inverse relationship between an earthquake’s depth and its magnitude.
lat when depth is greater than the median depth? What is the standard deviation of the variable lat when depth is less than the median depth? What does this suggest about the relationship between an earthquake’s latitude and it’s depth?sd(quakes$lat[quakes$depth>median(quakes$depth)])
## [1] 3.577252
sd(quakes$lat[quakes$depth<median(quakes$depth)])
## [1] 6.1501
There is an inverse relationship between an earthquake’s latitude and it’s depth.
depth is measured in kilometers. Create a new variable called depth_m that gives depth in meters rather than kilometers and add it to the dataset quakes. To help get you started, I have given you code that creates the new variable but fills it with NA values. Overwrite the NAs below by writing code on the right-hand side of the assignment operator (<-) that computes the requested transformation. Print out the first few rows of the updated dataset using head().quakes$depth_m <- quakes$depth*1000
head(quakes$depth_m, n= 10)
## [1] 562000 650000 42000 626000 649000 195000 82000 194000 211000 622000
depth using the boxplot() function. Describe where you see the min, max, and median (which you calculated in question 2) in this plot.boxplot(quakes$depth, main="Depth Boxplot")
The max is on top whisker and the minimum value is the bottom whisker. The median is the line within the box.
depth using the hist() function. What important information does the histogram provide that the boxplot does not?hist(quakes$depth, main="Depth Histogram",xlab="Depths")
Histogram displays better distribution of depth. From histogram we can tell if the distribution is symmetric or skewed. The distribution in the depth histogram is bimodal.
mag and stations against each other using the plot() function. Note that to generate a scatterplot, the plot() takes two arguments: the x-axis variable and the y-axis variable. Describe the relationship between the two variables.plot(quakes$mag, quakes$stations, main = "Scatterplot of Mag and Stations",
xlab = "mag", ylab = "stations")
There is a positive linear relationship between mag and stations with some data point being far away as mag and stations values increase.
long on the x-axis and lat on the y-axis. Using this plot, and the map/link below (note the two trenches), and some of the techniques you practiced above, are deeper quakes more likely to originate east or west of Fiji? Link to location on Google maps
plot(x=quakes$long, y=quakes$lat, main = "Quakes' geographic locations",
xlab = "Longitude", ylab = "latitude")
mean(quakes$depth[quakes$long>175])
## [1] 347.9182
mean(quakes$depth[quakes$long<175])
## [1] 169.639
Deeper quakes more likely to originate east of Fiji with the mean depth on longitude greater than 175 appears to be 374.9182 and higher in quantity.
## R version 4.0.2 (2020-06-22)
## Platform: x86_64-w64-mingw32/x64 (64-bit)
## Running under: Windows 10 x64 (build 18363)
##
## Matrix products: default
##
## locale:
## [1] LC_COLLATE=English_United States.1252
## [2] LC_CTYPE=English_United States.1252
## [3] LC_MONETARY=English_United States.1252
## [4] LC_NUMERIC=C
## [5] LC_TIME=English_United States.1252
##
## attached base packages:
## [1] stats graphics grDevices utils datasets methods base
##
## loaded via a namespace (and not attached):
## [1] compiler_4.0.2 magrittr_1.5 tools_4.0.2 htmltools_0.5.0
## [5] yaml_2.2.1 stringi_1.4.6 rmarkdown_2.6 knitr_1.29
## [9] stringr_1.4.0 xfun_0.16 digest_0.6.25 rlang_0.4.7
## [13] evaluate_0.14
## [1] "2021-01-29 19:32:20 CST"
## sysname release version nodename machine
## "Windows" "10 x64" "build 18363" "MSI" "x86-64"
## login user effective_user
## "andyv" "andyv" "andyv"