date()
## [1] "Thu Sep 26 13:35:35 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.
rainfall = 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(rainfall)
## [1] 7.537
median(rainfall)
## [1] 6.085
c. How many Junes had rainfall exceeding 5.5 inches?
sum(rainfall > 5.5)
## [1] 6
d. Compute the change in rainfall from one June to next.
diff(rainfall)
## [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(rainfall[4:8])
## [1] 7.345
2 The data set pi2000 (UsingR) contains the digits of pi.
require(UsingR)
## Loading required package: UsingR Loading required package: MASS
a. How many digits are there?
length(pi2000)
## [1] 2000
b. How many times does the digit 9 appear?
sum(pi2000 == 9)
## [1] 211
c. What digit is most common?
which.max(pi2000)
## [1] 6
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 = c(0.1, 0.03, 0.19, 0.12, 0.04, 0.095, 0.07, 0.06, 0.02, 0.05)
Beers = data.frame(bc = BC, bal = BAL)
b. Make a scatter plot of these data with BC on the horizontal axis and include axes labels.
require(ggplot2)
## Loading required package: ggplot2
##
## Attaching package: 'ggplot2'
##
## The following object is masked from 'package:UsingR':
##
## movies
ggplot(Beers, aes(x = bc, y = bal)) + geom_point() + xlab("Total Beers Consumed") +
ylab("Blood Alcholo Level")
c. Find the Spearman rank correlation between the two variables.
cor(Beers$bc, Beers$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?
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() + xlab("Site") +
ylab("Carbon Monoxide Levels")
5 Download and plot a road map of Sofia, Bulgaria. Use a zoom of 13.
require(ggmap)
## Loading required package: ggmap
##
## Attaching package: 'ggmap'
##
## The following object is masked from 'package:UsingR':
##
## crime
library(ggmap)
Sofia = get_map(location = "Sofia Bulgaria", maptype = "road", zoom = 13, source = "google")
## Map from URL :
## http://maps.googleapis.com/maps/api/staticmap?center=Sofia+Bulgaria&zoom=13&size=%20640x640&scale=%202&maptype=roadmap&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)