0

Under hospital mortality rate of pneumonia (below), row 104 has a value of 9.7 and row 99 has a value of 10.5. When Running which.min, hospital name that is returned is row 99 and not row 104. Why is that?

R version and System:

-Windows 10
-R-3.6.1

Calling the function:

best("AK","pneumonia")

Using this function:

best <- function(state, outcome) {
     #read file function

  #Reads the csv file
  dataTable  <- read.csv("outcome.csv", header = TRUE)
  dataTable
  #Passes the state argument to the choice variable
  choice <- state
  #selects all rows which match the state that was selected
  stateOfChoice <- dataTable[dataTable$State == choice,]
  stateOfChoice

  #Makes sure that only three of outcomes found in the csv file are selected
  if(outcome != "heart failure" && outcome != "heart attack" && outcome != "pneumonia"){
    print("wrong condition, try again")
    main()
  }

  #using the selected rows from above, return the minimum value of rate from heart attack and then use this selected row to find the hospital name
  else if (outcome == "heart attack"){


    heart_attack <- stateOfChoice[which.min(stateOfChoice$Hospital.30.Day.Death..Mortality..Rates.from.Heart.Attack),]
    heart_attack
    hospital <- heart_attack$Hospital.Name
    hospital
    return(hospital)
  }

  #Similar as above, but instead with heart failure
  else if (outcome == "heart failure"){
    heart_failure <- stateOfChoice[which.min(stateOfChoice$Hospital.30.Day.Death..Mortality..Rates.from.Heart.Failure),]
    hospital <- heart_failure$Hospital.Name
    return(hospital)
  }

  #Similar as above, but instead with pneumonia
  else if (outcome == "pneumonia"){
    pneumonia <- stateOfChoice[which.min(stateOfChoice$Hospital.30.Day.Death..Mortality..Rates.from.Pneumonia),]
    pneumonia
    hospital <- pneumonia$Hospital.Name
    return(hospital)
  }

}

Hospital Mortality Rate Pneumonia:

 Hospital.30.Day.Death..Mortality..Rates.from.Pneumonia
99                                                    10.5
100                                                   12.1
101                                                   11.6
102                                                   13.4
103                                                   12.5
104                                                    9.7

Hospital Name:

Provider.Number                                    Hospital.Name                        Address.1 Address.2
99           020001                 PROVIDENCE ALASKA MEDICAL CENTER                       BOX 196604        NA
100          020006                   MAT-SU REGIONAL MEDICAL CENTER        2500 SOUTH WOODWORTH LOOP        NA
101          020008                       BARTLETT REGIONAL HOSPITAL                 3260 HOSPITAL DR        NA
102          020012                      FAIRBANKS MEMORIAL HOSPITAL               1650 COWLES STREET        NA
103          020017                         ALASKA REGIONAL HOSPITAL                 2801 DEBARR ROAD        NA
104          020018               YUKON KUSKOKWIM DELTA REG HOSPITAL                       PO BOX 287        NA

Here is the link to the dataset:

Dataset

Matthew
  • 45
  • 1
  • 9
  • What does `class(stateOfChoice$Hospital.30.Day.Death..Mortality..Rates.from.Pneumonia)` return? – MrFlick Oct 14 '19 at 21:01
  • It returns type 'factor' – Matthew Oct 14 '19 at 21:15
  • 2
    Then you likely read your data in incorrectly (there is probably a non-numeric value in that column). Factors sort differently than numeric values. The error lies outside of any of the code you have shared here. When asking for help, make sure to include a proper [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – MrFlick Oct 14 '19 at 21:16
  • I went ahead and filled in those 'Not Available' slots with values of 200 to test and still same result. Likely, upstream of it, its still registering as type factor. Coercing it into numeric as.numeric might be of help? I've added a dataset link. – Matthew Oct 15 '19 at 04:39

0 Answers0