date()
## [1] "Fri Oct 05 02:53:49 2012"
Due Date: October 5, 2012, 2pm
Total Points: 30; Each question is worth 6 points.
(1) Create a vector called x with elements 2, 3, -5, -9, 3.4, 2.1, 18, -4, -7. Determine the mean, standard deviation, and variance on these values. Extract the third and fifth element of the vector.
x <- c(2, 3, -5, -9, 3.4, 2.1, 18, -4, -7)
mean(x)
## [1] 0.3889
sd(x)
## [1] 8.082
var(x)
## [1] 65.33
x[c(3, 5)]
## [1] -5.0 3.4
(2) Create another vector called y with elements 1.7, 1.1, 2.8, -3.2, -2.1, 5, 9, -19, 0. Compute the correlation and covariance between x and y.
y <- c(1.7, 1.1, 2.8, -3.2, -2.1, 5, 9, -19, 0)
# correlation between x and y
cor(x, y)
## [1] 0.5225
# covariance between x and y
cov(x, y)
## [1] 33.09
(3) Using the data frame airquality, compute the mean and standard deviation of the ozone concentration.
# dataframe 'airquality' use indexing device $ to extract column vector
# ex: airquality$Ozone. To perform operations ->
# operator(data.fram$column.name, na.rm=TRUE)
# mean of airquality
mean(airquality$Ozone, na.rm = TRUE)
## [1] 42.13
# standard deviation of airquality
sd(airquality$Ozone, na.rm = TRUE)
## [1] 32.99
(4) Create a sequence of values from 1.1 to 5.5, by .1. Determine the length of this sequence.
# define sequenc as a
a <- seq(1.1, 5.5, by = 0.1)
# show sequence
a
## [1] 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 2.0 2.1 2.2 2.3 2.4 2.5 2.6 2.7
## [18] 2.8 2.9 3.0 3.1 3.2 3.3 3.4 3.5 3.6 3.7 3.8 3.9 4.0 4.1 4.2 4.3 4.4
## [35] 4.5 4.6 4.7 4.8 4.9 5.0 5.1 5.2 5.3 5.4 5.5
# length of sequence
length(a)
## [1] 45
(5) Import the US.txt data file from Blackboard into R and compute the annual average number of Florida hurricanes.
H = read.table("US.txt", header = TRUE)
# annual average number of Hurricanes using $ operator
mean(H$FL, rm.na = TRUE)
## [1] 0.6813
# number of years
length(H$FL)
## [1] 160
# same using attach()
attach(H)
n = length(FL)
rate = mean(FL)
n
## [1] 160
rate
## [1] 0.6813