date()
## [1] "Thu Sep 26 13:28:19 2013"
Due Date/Time: 1:45pm Each question is worth 20 points.
1 The average rainfall (inches) over Florida during the month of June for the years 2005-2012 in order are:
11.824 5.752 5.998 6.991 6.171 5.440 4.965 13.156
a. Input these data into R.
rain = as.numeric(c(11.824, 5.752, 5.998, 6.991, 6.171, 5.44, 4.965, 13.156))
b. What is the mean and median of the June rainfall over these years?
mean(rain)
## [1] 7.537
median(rain)
## [1] 6.085
c. How many Junes had rainfall exceeding 5.5 inches?
length(which(rain > 5.5))
## [1] 6
d. Compute the change in rainfall from one June to next.
diff(rain)
## [1] -6.072 0.246 0.993 -0.820 -0.731 -0.475 8.191
e. What is the average June rainfall since 2008?
mean(rain[4:8])
## [1] 7.345
2 The data set pi2000 (UsingR) contains the digits of pi.
a. How many digits are there?
library(UsingR)
## Loading required package: MASS
length(pi2000)
## [1] 2000
b. How many times does the digit 9 appear?
length(which(pi2000 == 9))
## [1] 211
c. What digit is most common?
names(which.max(sort(table(pi2000))))
## [1] "1"
3 Obviously the more beer you drink (BC), the more your blood alcohol level (BAL) rises. Suppose we have the following data on student beer consumption
BC = c(5, 2, 9, 8, 3, 7, 3, 5, 3, 5) BAL = c(.10, .03, .19, .12, .04, .095, .07, .06, .02, .05)
a. Create a data frame and call it 'Beers'.
BC = c(5, 2, 9, 8, 3, 7, 3, 5, 3, 5)
BAL = as.numeric(c(0.1, 0.03, 0.19, 0.12, 0.04, 0.095, 0.07, 0.06, 0.02, 0.05))
Beers <- (data.frame(a = BC, b = BAL))
names(Beers) <- c("NumBeers", "BAC")
b. Make a scatter plot of these data with BC on the horizontal axis and include axes labels.
library(ggplot2)
## Attaching package: 'ggplot2'
##
## The following object is masked from 'package:UsingR':
##
## movies
ggplot(Beers, aes(x = Beers$NumBeers, y = Beers$BAC)) + geom_point() + xlab("Number of beers") +
ylab("Blood Alcohol Level")
c. Find the Spearman rank correlation between the two variables.
cor(BC, BAL, method = "spearman")
## [1] 0.8512
4 The data frame carbon (UsingR) contains a list of carbon monoxide levels at three different measuring sites.
a. How many rows and how many columns does the data frame contain?
library(UsingR)
dim(carbon)
## [1] 24 2
b. Print the names of the columns.
names(carbon)
## [1] "Monoxide" "Site"
c. Print the first six rows the the data frame.
head(carbon)
## Monoxide Site
## 1 0.106 1
## 2 0.127 1
## 3 0.132 1
## 4 0.105 1
## 5 0.117 1
## 6 0.109 1
d. Create side-by-side box plots of the monoxide levels from the three sites.
ggplot(carbon, aes(x = factor(Site), y = Monoxide)) + geom_boxplot()
5 Download and plot a road map of Sofia, Bulgaria. Use a zoom of 13.
library(ggmap)
## Attaching package: 'ggmap'
##
## The following object is masked from 'package:UsingR':
##
## crime
Sofia = get_map(location = "Sofia,Bulgaria", maptype = "satellite", zoom = 13,
source = "google")
## Map from URL :
## http://maps.googleapis.com/maps/api/staticmap?center=Sofia,Bulgaria&zoom=13&size=%20640x640&scale=%202&maptype=satellite&sensor=false
## Google Maps API Terms of Service : http://developers.google.com/maps/terms
## Information from URL :
## http://maps.googleapis.com/maps/api/geocode/json?address=Sofia,Bulgaria&sensor=false
## Google Maps API Terms of Service : http://developers.google.com/maps/terms
ggmap(Sofia)