Each problem below is worth 2 points.
For this problem set, you are allowed to help each other. However, please list who you’ve worked with and do not post answers in slack.
Please answer the following questions. Once complete, knit the Rmarkdown to .html and submit your .html document to canvas.
The problem set is due 5pm on Dec 1th
Using the state.area vector, print the 1st, 10th and
50th value of the vector
data(state)
state.area[c(1,10,50)]
## [1] 51609 58876 97914
What is the total area of the states in the 1st, 10ths and 50th
positions in the state.area vector ?
sum(state.area[c(1,10,50)])
## [1] 208399
What is the area of the largest state?
max(state.area)
## [1] 589757
What is the median size of the 10 largest states by area?
median(sort(state.area, decreasing = TRUE)[1:10])
## [1] 117787.5
What is size of the 3rd smallest state?
sort(state.area)[3]
## [1] 5009
What is the name of the 3rd smallest state by area?
states <- as.data.frame(state.name)
states$states.abb <- state.abb
states$region <- state.region
states$division <- state.division
states$area <- state.area
states <- states[order(states$area),]
states$state.name[3]
## [1] "Connecticut"
How many states have an area greater than 10,000 and less than 500,000?
length(state.area[state.area > 10000 & state.area < 500000])
## [1] 41
Read the documentation for the startsWith function.
Using this function, answer the following:
How many state abbreviations (state.abb vector) start
with N?
length(which(startsWith(state.abb, 'N')))
## [1] 8
What is the total area of states whose abbreviation starts with
C?
sum(state.area[(startsWith(state.abb, 'C'))])
## [1] 267949
The state.x77 dataset containing various statistics on each state.
Answer the following in text or code. Consider reading the documentation
for assistance (?state.x77).
What data structure (e.g. vector, matrix, array, list, etc.) and what
data type(s) (e.g. logical, numeric/double, character) are in the
state.x77 object ? (1 point)
Convert the state.x77 to a data.frame, if needed, and answer the following:
How many states have more than 120 days of frost on average (see the
Frost column)?
# The data structure is a matrix and array and the data type is a double
state.x77.df <- as.data.frame(state.x77)
sum(state.x77.df$Frost >= 120)
## [1] 24
What is the average population of the Western states? (consider using the state.region vector to assist)
regions <- as.data.frame(state.region)
state.x77.df$Region <- state.region
state.x77.df[state.x77.df$Region == 'West',]
## Population Income Illiteracy Life Exp Murder HS Grad Frost Area
## Alaska 365 6315 1.5 69.31 11.3 66.7 152 566432
## Arizona 2212 4530 1.8 70.55 7.8 58.1 15 113417
## California 21198 5114 1.1 71.71 10.3 62.6 20 156361
## Colorado 2541 4884 0.7 72.06 6.8 63.9 166 103766
## Hawaii 868 4963 1.9 73.60 6.2 61.9 0 6425
## Idaho 813 4119 0.6 71.87 5.3 59.5 126 82677
## Montana 746 4347 0.6 70.56 5.0 59.2 155 145587
## Nevada 590 5149 0.5 69.03 11.5 65.2 188 109889
## New Mexico 1144 3601 2.2 70.32 9.7 55.2 120 121412
## Oregon 2284 4660 0.6 72.13 4.2 60.0 44 96184
## Utah 1203 4022 0.6 72.90 4.5 67.3 137 82096
## Washington 3559 4864 0.6 71.72 4.3 63.5 32 66570
## Wyoming 376 4566 0.6 70.29 6.9 62.9 173 97203
## Region
## Alaska West
## Arizona West
## California West
## Colorado West
## Hawaii West
## Idaho West
## Montana West
## Nevada West
## New Mexico West
## Oregon West
## Utah West
## Washington West
## Wyoming West
West <- state.x77.df[state.x77.df$Region == 'West',]
mean(West$Population)
## [1] 2915.308