I am reading data from a csv file which has 3 columns (hospital name- character, state-character, mortality rate-numeric) by:
datafile <- read.csv("outcome-of-care-measures.csv", 
    na.strings = "Not Available",
    colClasses = c("character","character","numeric"))
Now I split the data based on state:
## split data based on state name
data_split <- split(datafile,datafile$State) 
My problem is to find the “worst” hospital (highest mortality rate) in each state and display the result. For this, first I sorted the data”: (rate is a list)
for (i in 1:length(data_split)){
  ## remove all rows with NA
  rate[[i]] <- data_split[[i]][complete.cases(data_split[[i]][ ,3]), ]  
  ##sort by mortality and remove
  ## conflict by hospital name
  rate[[i]] <- rate[[i]][order(rate[[i]][, 3],rate[[i]][ ,1]), ]  
}
Program is working but I am getting the wrong hospital name for many states. I am unable to find error in the program.
 
     
    