Problem Set # 1

Andrew Green

date()
## [1] "Thu Sep 10 16:41:01 2020"

Due Date: September 13, 2020

Total Points: 42

library(ggplot2)

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)
hurricanes = c(0, 1, 1, 1, 0, 2, 2, 1, 3, 3, 0, 0, 1, 2, 6, 6, 0, 1, 3, 0, 1)
hurricanes
##  [1] 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(hurricanes)
## [1] 21
year = 1990:2010
hurricanes2.df = data.frame(year, hurricanes)
(hurricanes2.df)
##    year hurricanes
## 1  1990          0
## 2  1991          1
## 3  1992          1
## 4  1993          1
## 5  1994          0
## 6  1995          2
## 7  1996          2
## 8  1997          1
## 9  1998          3
## 10 1999          3
## 11 2000          0
## 12 2001          0
## 13 2002          1
## 14 2003          2
## 15 2004          6
## 16 2005          6
## 17 2006          0
## 18 2007          1
## 19 2008          3
## 20 2009          0
## 21 2010          1
  1. What is the total number of hurricanes over all years? (2)
sum(hurricanes)
## [1] 34

2 Answer the following questions by typing R commands.

  1. Create a vector of numbers starting with 0 and ending with 25. (2)
"x" = seq(0, 25)
x
##  [1]  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
## [26] 25
  1. What is the length of this vector? (2)
length(x)
## [1] 26
  1. Create a new vector from the original vector by subtracting the mean value over all numbers in the vector. (2)
mean(x)
## [1] 12.5
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)
mileage = c(65311, 65624, 65908, 66219, 66499, 66821, 67145, 67447)
mileage
## [1] 65311 65624 65908 66219 66499 66821 67145 67447
  1. Use the function diff() to determine the number of miles between fill-ups. (2)
diff = diff(mileage)
diff
## [1] 313 284 311 280 322 324 302
  1. What is the maximum, minimum, and mean number of miles between fill-ups? (3)
range(diff)
## [1] 280 324
mean(diff)
## [1] 305.1429

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(1,100, 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 = c (rep(1,3), rep(2,3), rep(3,3))
rep
## [1] 1 1 1 2 2 2 3 3 3
  1. 1, 1, 1, 2, 2, 3 (2)
rep2 = c (rep(1,3), rep(2,2), rep(3,1))
rep2
## [1] 1 1 1 2 2 3
  1. 1, 2, 3, 4, 5, 4, 3, 2, 1 (3) Hint: Use the c() function.
rep3 = c (rep(1:5),rep(4:1))
rep3
## [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).

loc = "https://moraviansoundscapes.music.fsu.edu/sites/g/files/upcbnu1806/files/Media/Sciuchetti/ALMonthlyP.txt"
AL = read.table(file = loc, header = TRUE)
head(AL)
##   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)
min(AL$Jan)
## [1] 0.8
max(AL$Jan)
## [1] 13.09
  1. Sort the February rainfall values from wettest to driest. (2)
sort(AL$Feb)
##   [1]  0.76  1.25  1.29  1.32  1.34  1.39  1.41  1.45  1.86  2.09  2.14  2.23
##  [13]  2.29  2.37  2.39  2.45  2.48  2.52  2.60  2.69  2.71  2.73  2.82  2.91
##  [25]  2.92  3.00  3.13  3.16  3.21  3.22  3.24  3.24  3.25  3.35  3.37  3.40
##  [37]  3.43  3.46  3.53  3.56  3.56  3.60  3.60  3.60  3.68  3.72  3.73  3.74
##  [49]  3.81  3.82  3.87  3.94  4.05  4.11  4.23  4.26  4.27  4.31  4.32  4.34
##  [61]  4.38  4.39  4.41  4.43  4.47  4.52  4.53  4.69  4.74  4.75  4.80  4.84
##  [73]  4.89  4.92  4.94  4.98  5.02  5.04  5.04  5.08  5.13  5.17  5.29  5.49
##  [85]  5.56  5.59  5.63  5.80  5.90  6.02  6.04  6.09  6.10  6.12  6.39  6.56
##  [97]  6.56  6.57  6.64  6.70  7.02  7.05  7.14  7.46  7.50  7.56  7.80  7.89
## [109]  8.13  8.24  8.46  8.57  8.60  8.76  9.08  9.23  9.26  9.58  9.85 10.13
## [121] 10.18 10.69 11.42 12.16 13.35
  1. Compute the variance of the March rainfall values. (2)
var(AL$Mar)
## [1] 7.41769
  1. What is the 95th percentile value of April rainfall? (2)
quantile(AL$Apr, c(.95))
##   95% 
## 10.21
  1. Create a time series graph of April rainfall. (4)
ggplot(AL, aes(x = Year, y = Apr)) +
  geom_line() + 
  ylab("April Precipitation in Alabama (in)")