Question

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.

Was it helpful?

Solution

Why split the data.frame?

something like this would help?

df <- data.frame('hospital' = LETTERS[1:6],
                 'state' = rep(c('state1', 'state2', 'state3'),2),
                 'mr' = c(1:6))
df
  hospital  state mr
1        A state1  1
2        B state2  2
3        C state3  3
4        D state1  4
5        E state2  5
6        F state3  6

df2 <- df[with(df, order(-mr, state)), ]

df2[!duplicated(df2$state), ]
  hospital  state mr
6        F state3  6
5        E state2  5
4        D state1  4

You could do it with your approach but maintaining the list with all initial complete entries. But why?

ds <- split(df, df$state) 
rate <- list()
for (i in 1:length(ds)){
  ## remove all rows with NA
  rate[[i]] <- ds[[i]][complete.cases(ds[[i]][ ,3]), ]  
  ##sort by mortality and remove
  ## conflict by hospital name
  rate[[i]] <- rate[[i]][order(- rate[[i]][, 3], rate[[i]][ ,1]), ]  

}
rate

 [[1]]
  hospital  state mr
4        D state1  4
1        A state1  1

[[2]]
  hospital  state mr
5        E state2  5
2        B state2  2

[[3]]
  hospital  state mr
6        F state3  6
3        C state3  3
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top