setwd("/Users/tt/Documents")
grad <-read.csv("grad-rate2.csv", colClasses="character")
graddat <- data.frame(grad)
head(graddat)
## School City..State X4.year.graduation.rate
## 1 Juilliard School New York, NY 94%
## 2 Bowdoin College Brunswick, ME 91%
## 3 College of the Holy Cross Worcester, MA 91%
## 4 Amherst College Amherst, MA 90%
## 5 Babson College Babson Park, MA 90%
## 6 Georgetown University Washington, DC 90%
names(graddat)
## [1] "School" "City..State"
## [3] "X4.year.graduation.rate"
graddat$rate <- as.numeric(gsub("%","",graddat$X4.year.graduation.rate))
str(graddat)
## 'data.frame': 1249 obs. of 4 variables:
## $ School : chr "Juilliard School" "Bowdoin College" "College of the Holy Cross" "Amherst College" ...
## $ City..State : chr "New York, NY" "Brunswick, ME" "Worcester, MA" "Amherst, MA" ...
## $ X4.year.graduation.rate: chr "94%" "91%" "91%" "90%" ...
## $ rate : num 94 91 91 90 90 90 90 90 90 90 ...
#graddat<- graddat[,c(1,4,6,7)]
graddat$n_chars = nchar(graddat$City..State)
str(graddat$n_chars)
## int [1:1249] 12 13 13 11 15 14 11 15 13 14 ...
graddat$city = as.character(substr(graddat$City..State, 1, graddat$n_chars-4))
graddat$state = as.character(substr(graddat$City..State, graddat$n_chars-1, graddat$n_chars))
hist(graddat[,4],breaks= 100, xlab = "Graduation Rates", main="Top Graduation Rates by State", col = 'skyblue',border = 'white')

## [1] "School" "City..State"
## [3] "X4.year.graduation.rate"
graddat2<- graddat[,c(1,4,7,6)]
best<-function(state1){
stdat <-data.frame(graddat2)
if (!state1 %in% unique(stdat$state)){stop("invalid state")}
stdf <- stdat[stdat$state == state1,]
stdf <- na.omit(stdf)
max_stdf<- stdf[which.max(stdf[,2]),1]
print(max_stdf)
}
worst<-function(state1){
stdat <-data.frame(graddat2)
if (!state1 %in% unique(stdat$state)){stop("invalid state")}
stdf <- stdat[stdat$state == state1,]
stdf <- na.omit(stdf)
min_stdf<- stdf[which.min(stdf[,2]),1]
print(min_stdf)
}
rankcollege<- function(state2, num="best"){
bsdat <-data.frame(graddat2)
if (!state2 %in% unique(bsdat$state)){stop("invalid state")}
bsdf <- bsdat[bsdat$state == state2,]
bsdf <- na.omit(bsdf)
ncollege<-nrow(bsdf)
switch(num, "best" = {num = ncollege}, "worst" = {num = 1} )
if (num > ncollege) {result = NA}
order_bsdf <- order(bsdf[,2], bsdf[ ,1])
rank_bsdf <- bsdf[order_bsdf, ][num ,1]
rank_bsdf
}
rank_allstates <- function(num="best") {
alldat <- data.frame(graddat2)
allstates <- unique(alldat[, 3])
print(head(allstates))
alldat <- na.omit(alldat)
in_state_rank<- function(state3){
in_statedf <- alldat[alldat[,3] == state3,]
mcollege<-nrow(in_statedf)
switch(num, "best" = {num = mcollege}, "worst" = {num = 1} )
if (num > mcollege) {result = NA}
order_in_statedf <- order(in_statedf[,2], in_statedf[ ,1])
result <- in_statedf[order_in_statedf, ][num ,1]
in_state<- c(result, state3)
head(in_state,3)
}
output = do.call(rbind, lapply(allstates, in_state_rank))
output = output[order(output[ ,2]), ]
row.names(output) = output[ ,2]
colnames(output) = c("college", "state")
data.frame(output)
}
best("TX")
## [1] "Rice University"
best("NY")
## [1] "Juilliard School"
worst("TX")
## [1] "University of Houston--Downtown"
worst("NY")
## [1] "CUNY--Medgar Evers College"
rankcollege("NY",4)
## [1] "CUNY--City College"
rankcollege("NY","best")
## [1] "Juilliard School"
rankcollege("NY","worst")
## [1] "CUNY--Medgar Evers College"
rankcollege("TX",10)
## [1] "Texas A&M University--Kingsville"
rankcollege("TX","best")
## [1] "Rice University"
rankcollege("TX","worst")
## [1] "University of Houston--Downtown"
head(rank_allstates("best"), 10)
## [1] "NY" "ME" "MA" "DC" "CA" "MD"
## college state
## AK University of Alaska--Fairbanks AK
## AL Samford University AL
## AR Hendrix College AR
## AZ University of Arizona AZ
## CA Soka University of America CA
## CO Colorado College CO
## CT Yale University CT
## DC Georgetown University DC
## DE University of Delaware DE
## FL University of Miami FL
tail(rank_allstates("worst"), 3)
## [1] "NY" "ME" "MA" "DC" "CA" "MD"
## college state
## WI University of Wisconsin--Parkside WI
## WV West Virginia State University WV
## WY University of Wyoming WY
head(rank_allstates(2), 10)
## [1] "NY" "ME" "MA" "DC" "CA" "MD"
## college state
## AK University of Alaska--Anchorage AK
## AL Alabama Agricultural and Mechanical University AL
## AR University of Arkansas--Pine Bluff AR
## AZ Embry-Riddle Aeronautical University--Prescott AZ
## CA California State University--Fullerton CA
## CO Metropolitan State University of Denver CO
## CT Western Connecticut State University CT
## DC University of the District of Columbia DC
## DE Wilmington University DE
## FL Bethune-Cookman University FL