setwd("C:/Users/amp12/Downloads")
grad<- read.csv("grad-rate.csv", colClasses = "character")

ncol(grad)
## [1] 3
nrow(grad)
## [1] 1252
names(grad)
## [1] "School"                  "City..State"            
## [3] "X4.year.graduation.rate"
str(grad)
## 'data.frame':    1252 obs. of  3 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%" ...
#Split the city and state
grad$rate=gsub("%", "", grad$X4.year.graduation.rate)
grad$n_char<-nchar(grad$City..State)
grad$State<-substr(grad$City..State, grad$n_char-1, grad$n_char)
State<-data.frame(grad$State)
grad$City<-substr(grad$City..State, grad$n_char-25, grad$n_char-4)
City<-data.frame(grad$City)

#Histogram of Graduation Rate
grad2<-grad[,c(1,4,6,7)]
str(grad2)
## 'data.frame':    1252 obs. of  4 variables:
##  $ School: chr  "Juilliard School" "Bowdoin College" "College of the Holy Cross" "Amherst College" ...
##  $ rate  : chr  "94" "91" "91" "90" ...
##  $ State : chr  "NY" "ME" "MA" "MA" ...
##  $ City  : chr  "New York" "Brunswick" "Worcester" "Amherst" ...
grad2[,2]<-as.numeric(grad2[,2])
hist(grad2[,2], breaks = 10, xlab="Graduation Rate", main ="Histogram of College Graduation Rates",col='skyblue', border='white') 

#Display the best college of a selected state
bestcollegeinstate<-function(State){
  datt<-data.frame(grad2)
  if (!State%in%unique(datt$State)){stop("invalid state")}
dff <- datt[datt$State == State, ] 
  new_daf <- dff[complete.cases(dff), ]
new_daf <- new_daf[order(new_daf[,2], decreasing = TRUE),]
new_daf<-new_daf[,1:3]
print(new_daf[1,1])
}
  
#Rank colleges in a selected state by graduation rate
instaterank <- function(State1) {         
stdat<- data.frame(grad2)
  if(!State1%in% unique(stdat$State)){stop("invalid state")}
df <- stdat[stdat$State == State1, ] 
# Removing Missing Values for numerical datatype (outcome column)
new_df <- df[complete.cases(df), ]
new_df <- new_df[order(new_df[,2], decreasing = TRUE),]
new_df<-new_df[,1:3]
print(new_df)

#List the top 5 colleges by graduation rate in the state
print(head(new_df,5))

#List the bottom 5 colleges by graduation rate in the state
print(tail(new_df,5))

#Find the best & worst college in the state(by graduation rate)
max_df <- df[which.max(new_df[,2]), 1] 
print(max_df)
min_df <- df[which.min(new_df[,2]), 1] 
print(min_df)
}

#Display the top 50 colleges in the country by graduation rate 
bestincountry <- function(State4) {         
alldat<- data.frame(grad2)
dff <- alldat[, ] 
#Removing Missing Values for numerical datatype (outcome column)
new_df <- dff[complete.cases(dff), ]
new_df <- new_df[order(new_df[,2], decreasing = TRUE),]
new_df<-new_df[1:50,1:3]
print(new_df)
}

bestincountry()
##                                   School rate State
## 1                       Juilliard School   94    NY
## 2                        Bowdoin College   91    ME
## 3              College of the Holy Cross   91    MA
## 4                        Amherst College   90    MA
## 5                         Babson College   90    MA
## 6                  Georgetown University   90    DC
## 7                       Hamilton College   90    NY
## 8             Soka University of America   90    CA
## 9            United States Naval Academy   90    MD
## 10              University of Notre Dame   90    IN
## 11                         Bates College   89    ME
## 12                      Carleton College   89    MN
## 13                         Colby College   89    ME
## 14                        Pomona College   89    CA
## 15                  Princeton University   89    NJ
## 16                    Swarthmore College   89    PA
## 17         Washington and Lee University   89    VA
## 18                        Boston College   88    MA
## 19                    Colgate University   88    NY
## 20                   Columbia University   88    NY
## 21                     Dartmouth College   88    NH
## 22                      Davidson College   88    NC
## 23                       Duke University   88    NC
## 24              Johns Hopkins University   88    MD
## 25                 University of Chicago   88    IL
## 26                University of Virginia   88    VA
## 27    Washington University in St. Louis   88    MO
## 28                       Barnard College   87    NY
## 29                    Bentley University   87    MA
## 30                     Haverford College   87    PA
## 31                      Tufts University   87    MA
## 32                  Villanova University   87    PA
## 33                       Yale University   87    CT
## 34                      Brown University   86    RI
## 35                   Bucknell University   86    PA
## 36                   Harvey Mudd College   86    CA
## 37                        Kenyon College   86    OH
## 38                     Lafayette College   86    PA
## 39                    Middlebury College   86    VT
## 40            University of Pennsylvania   86    PA
## 41                 Vanderbilt University   86    TN
## 42                      Williams College   86    MA
## 43           College of William and Mary   85    VA
## 44                    Cornell University   85    NY
## 45                    Macalester College   85    MN
## 46 Massachusetts Institute of Technology   85    MA
## 47                Santa Clara University   85    CA
## 48                      St. Olaf College   85    MN
## 49                        Vassar College   85    NY
## 50                   Wesleyan University   85    CT
instaterank("NY")
##                                                      School rate State
## 1                                          Juilliard School   94    NY
## 7                                          Hamilton College   90    NY
## 19                                       Colgate University   88    NY
## 20                                      Columbia University   88    NY
## 28                                          Barnard College   87    NY
## 44                                       Cornell University   85    NY
## 49                                           Vassar College   85    NY
## 56                                         Skidmore College   84    NY
## 68                                  St. Lawrence University   82    NY
## 72                           United States Military Academy   81    NY
## 87                                            Union College   79    NY
## 102                                 University of Rochester   77    NY
## 115                                      Yeshiva University   76    NY
## 117                       Hobart and William Smith Colleges   75    NY
## 119                                          Marist College   75    NY
## 120                                     New York University   75    NY
## 125                                      Fordham University   74    NY
## 126                                           Siena College   74    NY
## 128                             Binghamton University--SUNY   73    NY
## 135                                  Sarah Lawrence College   73    NY
## 149                   United States Merchant Marine Academy   72    NY
## 168                                     Syracuse University   70    NY
## 174          Albany College of Pharmacy and Health Sciences   69    NY
## 179                                          Ithaca College   69    NY
## 193                                           SUNY--Geneseo   68    NY
## 197                                          Webb Institute   68    NY
## 225                                          Monroe College   65    NY
## 237                                        Le Moyne College   64    NY
## 252                                 St. John Fisher College   63    NY
## 257                                          Wagner College   63    NY
## 259                                            Bard College   62    NY
## 262                                        Canisius College   62    NY
## 263                                            Cooper Union   62    NY
## 288                        Rensselaer Polytechnic Institute   61    NY
## 289                           St. Joseph's College New York   61    NY
## 299                                      Niagara University   60    NY
## 303                                   SUNY College--Oneonta   60    NY
## 316                                            Iona College   59    NY
## 320                                       Manhattan College   59    NY
## 324                              St. Bonaventure University   59    NY
## 330                                     Clarkson University   58    NY
## 350                                          Elmira College   57    NY
## 360                                  Purchase College--SUNY   57    NY
## 361                             University at Buffalo--SUNY   57    NY
## 372                                        Nazareth College   56    NY
## 376                              University at Albany--SUNY   56    NY
## 378                                      Adelphi University   55    NY
## 390                                   School of Visual Arts   55    NY
## 392                                         SUNY--New Paltz   55    NY
## 408                                  SUNY College--Cortland   54    NY
## 423                                      Hofstra University   53    NY
## 431                            Stony Brook University--SUNY   53    NY
## 457                                          Molloy College   51    NY
## 458                                          The New School   51    NY
## 485                                       Cazenovia College   49    NY
## 487                                        Hartwick College   49    NY
## 490                                  Manhattanville College   49    NY
## 504                              College at Brockport--SUNY   48    NY
## 514                                Roberts Wesleyan College   48    NY
## 518                                          SUNY--Fredonia   48    NY
## 527                                      The King's College   47    NY
## 548                                   College of Saint Rose   46    NY
## 554                                           Touro College   46    NY
## 561                                           Wells College   46    NY
## 572                                         Pratt Institute   45    NY
## 573                                       The Sage Colleges   45    NY
## 593                                    Paul Smith's College   44    NY
## 597                              St. Thomas Aquinas College   44    NY
## 598                                       SUNY--Plattsburgh   44    NY
## 616                                  Mount St. Mary College   43    NY
## 621  SUNY College of Agriculture and Technology--Cobleskill   43    NY
## 641                                       Alfred University   41    NY
## 646                                    CUNY--Baruch College   41    NY
## 689                             Marymount Manhattan College   39    NY
## 692                                         Pace University   39    NY
## 693                       SUNY College of Technology--Delhi   39    NY
## 730                            College of Mount St. Vincent   37    NY
## 735                                           Keuka College   37    NY
## 743                                   St. John's University   37    NY
## 744                      SUNY College of Technology--Alfred   37    NY
## 761                SUNY Polytechnic Institute--Albany/Utica   36    NY
## 765            Vaughn College of Aeronautics and Technology   36    NY
## 785                                           Utica College   35    NY
## 794                                             LIM College   34    NY
## 801                                   SUNY College--Potsdam   34    NY
## 820                                          Daemen College   32    NY
## 851                                           Nyack College   31    NY
## 862                        New York Institute of Technology   30    NY
## 864                      SUNY College of Technology--Canton   30    NY
## 875                                        Berkeley College   29    NY
## 876                                    CUNY--Queens College   29    NY
## 877                                       Dominican College   29    NY
## 880                         Farmingdale State College--SUNY   29    NY
## 883                                                LIU Post   29    NY
## 897                                  CUNY--Brooklyn College   28    NY
## 911                       Rochester Institute of Technology   28    NY
## 912                                     St. Francis College   28    NY
## 932                              SUNY College--Old Westbury   27    NY
## 943                                      D'Youville College   26    NY
## 989                                    CUNY--Hunter College   24    NY
## 996                                           Mercy College   24    NY
## 1055                                      SUNY--Morrisville   21    NY
## 1063                         CUNY--College of Staten Island   20    NY
## 1064                                   CUNY--Lehman College   20    NY
## 1182                                     CUNY--City College   12    NY
## 1222                       Metropolitan College of New York    8    NY
## 1228                                     CUNY--York College    7    NY
## 1236                             CUNY--Medgar Evers College    6    NY
##                 School rate State
## 1     Juilliard School   94    NY
## 7     Hamilton College   90    NY
## 19  Colgate University   88    NY
## 20 Columbia University   88    NY
## 28     Barnard College   87    NY
##                                School rate State
## 1064             CUNY--Lehman College   20    NY
## 1182               CUNY--City College   12    NY
## 1222 Metropolitan College of New York    8    NY
## 1228               CUNY--York College    7    NY
## 1236       CUNY--Medgar Evers College    6    NY
## [1] "Juilliard School"
## [1] "CUNY--Medgar Evers College"
instaterank("TX")
##                                        School rate State
## 61                            Rice University   83    TX
## 148                        Trinity University   72    TX
## 162             Southern Methodist University   71    TX
## 184                Texas Christian University   69    TX
## 209                   Southwestern University   67    TX
## 229                      University of Dallas   65    TX
## 242                            Austin College   63    TX
## 294                         Baylor University   60    TX
## 342               University of Texas--Austin   58    TX
## 409     Texas A&M University--College Station   54    TX
## 446                   St. Edward's University   52    TX
## 451               University of Texas--Dallas   52    TX
## 475                     LeTourneau University   50    TX
## 499              Abilene Christian University   48    TX
## 539      St. Mary's University of San Antonio   47    TX
## 581                 Dallas Baptist University   44    TX
## 652                      Schreiner University   41    TX
## 773                 Hardin-Simmons University   35    TX
## 781                 Texas Lutheran University   35    TX
## 782                     Texas Tech University   35    TX
## 853          University of Mary Hardin-Baylor   31    TX
## 854                  University of St. Thomas   31    TX
## 860              Lubbock Christian University   30    TX
## 870                 University of North Texas   30    TX
## 881                Houston Baptist University   29    TX
## 886              Sam Houston State University   29    TX
## 892          University of the Incarnate Word   29    TX
## 903                        McMurry University   28    TX
## 933                    Texas State University   27    TX
## 955           Our Lady of the Lake University   26    TX
## 958        Stephen F. Austin State University   26    TX
## 966                   Angelo State University   25    TX
## 979                     University of Houston   25    TX
## 981                University of Texas--Tyler   25    TX
## 984                 West Texas A&M University   25    TX
## 1002           Texas A&M University--Commerce   24    TX
## 1003                Texas Wesleyan University   24    TX
## 1004 University of Texas of the Permian Basin   24    TX
## 1038       Texas A&M International University   22    TX
## 1040           University of Texas--Arlington   22    TX
## 1054        Southwestern Adventist University   21    TX
## 1069              Midwestern State University   20    TX
## 1086            East Texas Baptist University   19    TX
## 1093         Texas A&M University--Kingsville   19    TX
## 1094                 Texas Woman's University   19    TX
## 1112          Texas A&M University--Texarkana   18    TX
## 1138     Texas A&M University--Corpus Christi   16    TX
## 1153         University of Texas--San Antonio   15    TX
## 1176              Prairie View A&M University   13    TX
## 1226                Texas Southern University    8    TX
## 1229                 Jarvis Christian College    7    TX
## 1240               Wayland Baptist University    6    TX
## 1246          University of Houston--Downtown    3    TX
##                            School rate State
## 61                Rice University   83    TX
## 148            Trinity University   72    TX
## 162 Southern Methodist University   71    TX
## 184    Texas Christian University   69    TX
## 209       Southwestern University   67    TX
##                               School rate State
## 1176     Prairie View A&M University   13    TX
## 1226       Texas Southern University    8    TX
## 1229        Jarvis Christian College    7    TX
## 1240      Wayland Baptist University    6    TX
## 1246 University of Houston--Downtown    3    TX
## [1] "Rice University"
## [1] "University of Houston--Downtown"
bestcollegeinstate("NY")
## [1] "Juilliard School"
bestcollegeinstate("MD")
## [1] "United States Naval Academy"