Problem Set # 1

John Graffeo

date()
## [1] "Fri Sep 16 20:48:57 2022"
library(knitr)
library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.2 ──
## ✔ ggplot2 3.3.6     ✔ purrr   0.3.4
## ✔ tibble  3.1.8     ✔ dplyr   1.0.9
## ✔ tidyr   1.2.0     ✔ stringr 1.4.1
## ✔ readr   2.1.2     ✔ forcats 0.5.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()

Due Date: September 18, 2022

Total Points: 42

1 The following values are the annual number hurricanes that have hit the United States since 1990. Answer the questions by typing R commands.

0 1 1 1 0 2 2 1 3 3 0 0 1 2 6 6 0 1 3 0 1

  1. Enter the data into R. (2)
hc = c(0, 1, 1, 1, 0, 2, 2, 1, 3, 3, 0, 0, 1, 2, 6, 6, 0, 1, 3, 0, 1)
  1. How many years are there? (2)
length(hc)
## [1] 21

There are 21 years in the hurricane data set.

  1. What is the total number of hurricanes over all years? (2)
sum(hc)
## [1] 34

There was a total of 34 hurricanes during those 21 years.

2 Answer the following questions by typing R commands.

  1. Create a vector of numbers starting with 0 and ending with 25. (2)
x = c(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25)
  1. What is the length of this vector? (2)
length(x)
## [1] 26

The length of vector ‘x’ is 26.

  1. Create a new vector from the original vector by subtracting the mean value over all numbers in the vector. (2)
x2 = (x - mean(x))
x2
##  [1] -12.5 -11.5 -10.5  -9.5  -8.5  -7.5  -6.5  -5.5  -4.5  -3.5  -2.5  -1.5
## [13]  -0.5   0.5   1.5   2.5   3.5   4.5   5.5   6.5   7.5   8.5   9.5  10.5
## [25]  11.5  12.5

3 Suppose you keep track of your mileage each time you fill your car’s gas tank. At your last 8 fill-ups the mileage was

65311 65624 65908 66219 66499 66821 67145 67447

  1. Enter these numbers into a vector called miles. (2)
miles = c(65311, 65624, 65908, 66219, 66499, 66821, 67145, 67447)
  1. Use the function diff() to determine the number of miles between fill-ups. (2)
diff(miles)
## [1] 313 284 311 280 322 324 302

The vehicle drove 313 miles on the first tank, 284 miles on the 2nd, 311 miles on the 3rd, 280 miles on the 4th, 322 miles on the 5th, 324 miles on the 6th, and 302 miles on the 7th. We do not yet know how many miles will be driven on the 8th tank of gas.

  1. What is the maximum, minimum, and mean number of miles between fill-ups? (3)
maxmi = max(diff(miles))
minmi = min(diff(miles))
meanmi = mean(diff(miles))
maxmi
## [1] 324
minmi
## [1] 280
meanmi
## [1] 305.1429

The maximum miles driven between fillups was 324 miles, the minimum was 280 miles, and the mean was 305.1429 miles.

4 Create the following sequences using the seq() and rep() functions as appropriate.

  1. “a”, “a”, “a”, “a” (2)
rep("a", 4)
## [1] "a" "a" "a" "a"
  1. The odd numbers in the interval from 1 to 100 (2)
seq(from = 1, to = 100, by = 2)
##  [1]  1  3  5  7  9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49
## [26] 51 53 55 57 59 61 63 65 67 69 71 73 75 77 79 81 83 85 87 89 91 93 95 97 99
  1. 1, 1, 1, 2, 2, 2, 3, 3, 3 (2)
rep(1:3, each = 3)
## [1] 1 1 1 2 2 2 3 3 3
  1. 1, 1, 1, 2, 2, 3 (2)
c(rep(1,3), rep(2,2), 3)
## [1] 1 1 1 2 2 3
  1. 1, 2, 3, 4, 5, 4, 3, 2, 1 (3) Hint: Use the c() function.
c(seq(1:5),4,3,2,1)
## [1] 1 2 3 4 5 4 3 2 1

5 Read the monthly precipitation dataset from my website (https://moraviansoundscapes.music.fsu.edu/sites/g/files/upcbnu1806/files/Media/Sciuchetti/ALMonthlyP.txt).

webppt = "https://moraviansoundscapes.music.fsu.edu/sites/g/files/upcbnu1806/files/Media/Sciuchetti/ALMonthlyP.txt"
ppt = read.table(file = webppt, header = TRUE)
head(ppt)
##   Year  Jan  Feb   Mar   Apr  May   Jun  Jul  Aug  Sep  Oct  Nov  Dec
## 1 1895 7.37 1.41  7.17  2.72 3.06  4.04 4.58 4.00 3.41 2.28 1.83 5.83
## 2 1896 2.47 7.46  6.23  4.34 2.92  4.50 3.78 1.94 2.67 1.59 6.20 1.32
## 3 1897 3.85 3.74 14.40  4.99 2.87  2.12 3.93 3.66 0.03 1.74 2.13 8.54
## 4 1898 7.07 1.34  4.43  4.29 1.86  2.61 5.52 3.67 2.83 3.72 3.55 2.43
## 5 1899 5.79 6.39  9.93  2.99 1.50  2.22 6.04 3.44 0.57 1.85 3.93 7.28
## 6 1900 3.64 4.92  4.17 10.56 3.86 12.40 4.64 2.26 2.76 6.40 3.44 3.25
  1. What are the wettest and driest values during the month of January? (2)
wmax = max(ppt[2])
wmin = min(ppt[2])
wmax
## [1] 13.09
wmin
## [1] 0.8

The wettest and driest values for January are 13.09 and 0.8, respectively.

  1. Sort the February rainfall values from wettest to driest. (2)
sort(ppt$Feb, decreasing = TRUE)
##   [1] 13.35 12.16 11.42 10.69 10.18 10.13  9.85  9.58  9.26  9.23  9.08  8.76
##  [13]  8.60  8.57  8.46  8.24  8.13  7.89  7.80  7.56  7.50  7.46  7.14  7.05
##  [25]  7.02  6.70  6.64  6.57  6.56  6.56  6.39  6.12  6.10  6.09  6.04  6.02
##  [37]  5.90  5.80  5.63  5.59  5.56  5.49  5.29  5.17  5.13  5.08  5.04  5.04
##  [49]  5.02  4.98  4.94  4.92  4.89  4.84  4.80  4.75  4.74  4.69  4.53  4.52
##  [61]  4.47  4.43  4.41  4.39  4.38  4.34  4.32  4.31  4.27  4.26  4.23  4.11
##  [73]  4.05  3.94  3.87  3.82  3.81  3.74  3.73  3.72  3.68  3.60  3.60  3.60
##  [85]  3.56  3.56  3.53  3.46  3.43  3.40  3.37  3.35  3.25  3.24  3.24  3.22
##  [97]  3.21  3.16  3.13  3.00  2.92  2.91  2.82  2.73  2.71  2.69  2.60  2.52
## [109]  2.48  2.45  2.39  2.37  2.29  2.23  2.14  2.09  1.86  1.45  1.41  1.39
## [121]  1.34  1.32  1.29  1.25  0.76
  1. Compute the variance of the March rainfall values. (2)
var(ppt[4])
##         Mar
## Mar 7.41769

The variance in March rainfall values is 7.41769.

  1. What is the 95th percentile value of April rainfall? (2)
quantile(ppt$Apr, probs = 0.95)
##   95% 
## 10.21

The 95th percintile value of April rainfall is 10.21.

  1. Create a time series graph of April rainfall. (4)
ggplot(data = ppt) +
  geom_line(aes(x=Year, y=Apr)) +
  geom_point(aes(x=Year, y=Apr))