Problem Set # 1

Marcus Infanti

date()
## [1] "Thu Sep 13 00:36:00 2012"

Due Date: September 13, 2012
Total Points: 30

1 The following values are the annual number hurricanes that have hit the United States since 1990. Follow instructions and answer questions by typing the appropriate R commands.

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

a. Enter the data into R. (2)

counts = c(0, 1, 1, 1, 0, 2, 2, 1, 3, 3, 0, 0, 1, 2, 6, 6, 0, 1, 
    3, 0, 1)  # annual number hurricanes that have hit the United States since 1990

b. How many years are there? (2)

length(counts)  # length of data vector
## [1] 21

c. What is the total number of hurricanes over all years? (2)

sum(counts)  # total number of hurricanes making landfall
## [1] 34

2 Answer the following questions by typing the appropriate R commands.

a. Create a vector of numbers starting with 0 and ending with 25. (2)

counts.d2 = c(0:25)  # integers 0 through 25

b. What is the length of this vector? (2)

length(counts.d2)  # number of integers in counts.d2
## [1] 26

c. Create a new vector from the original vector by subtracting the mean
value over all numbers in the vector. (2)

x = counts.d2
xbar = mean(x)
x - xbar
##  [1] -12.5 -11.5 -10.5  -9.5  -8.5  -7.5  -6.5  -5.5  -4.5  -3.5  -2.5
## [12]  -1.5  -0.5   0.5   1.5   2.5   3.5   4.5   5.5   6.5   7.5   8.5
## [23]   9.5  10.5  11.5  12.5

3 Suppose you keep track of your mileage each time you fill up. At your last 8 fill-ups the mileage was

65311 65624 65908 66219 66499 66821 67145 67447

a. Enter these numbers into a vector called miles. (2)

miles = c(65311, 65624, 65908, 66499, 66821, 67145, 67447)  # mileage at each of last 8 fill-ups

b. Use the function diff() to determine the number of miles between fill-ups. (2)

diff(miles)  # amount of miles in between each fill-up
## [1] 313 284 591 322 324 302
dm = diff(miles)

c. What is the maximum, minimum, and mean number of miles between fill-ups? (3)

max(dm)  # most miles in between fill-ups
## [1] 591
min(dm)  # least miles in between fill-ups
## [1] 284
mean(dm)  # average miles traveled in between fill-ups
## [1] 356

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

a. “a”, “a”, “a”, “a” (2)

rep("a", times = 4)
## [1] "a" "a" "a" "a"

b. 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
## [24] 47 49 51 53 55 57 59 61 63 65 67 69 71 73 75 77 79 81 83 85 87 89 91
## [47] 93 95 97 99

c. 1, 1, 1, 2, 2, 2, 3, 3, 3 (2)

rep(c(1, 2, 3), c(3, 3, 3))
## [1] 1 1 1 2 2 2 3 3 3

d. 1, 1, 1, 2, 2, 3 (2)

rep(c(1, 2, 3), c(3, 2, 1))
## [1] 1 1 1 2 2 3

e. 1, 2, 3, 4, 5, 4, 3, 2, 1 (3) Hint: Use the c() function.

t = c(1, 2, 3, 4, 5, 4, 3, 2, 1)
t
## [1] 1 2 3 4 5 4 3 2 1