date()
## [1] "Thu Sep 26 13:36:12 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.
rainamount = c(11.824, 5.752, 5.998, 6.991, 6.171, 5.44, 4.965, 13.156)
rainamount
## [1] 11.824 5.752 5.998 6.991 6.171 5.440 4.965 13.156
b. What is the mean and median of the June rainfall over these years?
mean(rainamount)
## [1] 7.537
median(rainamount)
## [1] 6.085
c. How many Junes had rainfall exceeding 5.5 inches?
sum(rainamount > 5.5)
## [1] 6
d. Compute the change in rainfall from one June to next.
diffrain = diff(rainamount)
diffrain
## [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(rainamount[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?
sum(pi2000 == 9)
## [1] 211
c. What digit is most common?
pi.table = table(pi2000)
pimax = max(pi.table)
which(pi.table == pimax)
## 1
## 2
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, BAL)
Beers
## BC BAL
## 1 5 0.100
## 2 2 0.030
## 3 9 0.190
## 4 8 0.120
## 5 3 0.040
## 6 7 0.095
## 7 3 0.070
## 8 5 0.060
## 9 3 0.020
## 10 5 0.050
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 = BC, y = BAL)) + geom_point() + xlab("Beers Consumed") +
ylab("Blood Alcohol 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?
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 = Site, y = Monoxide)) + geom_boxplot() + facet_wrap(~Site) +
xlab("Site") + ylab("Carbon Monoxide Concentration") + theme_bw()
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
SofiaMap = get_map(location = "Sofia, Bulgaria", maptype = "roadmap", 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(SofiaMap, extent = "device")