Explanation
I'm trying to write a function which has to find the lowest number in one of this columns and return hospital name.
- "Hospital.30.Day.Death..Mortality..Rates.from.Heart.Attack"
- "Hospital.30.Day.Death..Mortality..Rates.from.Heart.Failure"
- "Hospital.30.Day.Death..Mortality..Rates.from.Pneumonia"
I cannot understand why my results are not the same like in samples from PDF. Please hold in mind that I'm a fresh R programmer.
Example
For best("TX", "heart attack")
function should return "CYPRESS FAIRBANKS MEDICAL CENTER". While my function returns: (Pay attention that correct result isn't even in this vector)
[1] "HEREFORD REGIONAL MEDICAL CENTER"
[2] "EAST TEXAS MEDICAL CENTER MOUNT VERNON"
[3] "ALLEGIANCE SPECIALTY HOSPITAL OF KILGORE"
[4] "KNOX COUNTY HOSPITAL"
[5] "EAST TEXAS MEDICAL CENTER TRINITY"
[6] "COMMUNITY GENERAL HOSPITAL"
[7] "KELL WEST REGIONAL HOSPITAL"
[8] "GOOD SHEPHARD MEDICAL CENTER-LINDEN"
[9] "BURLESON ST JOSEPH HEALTH CENTER"
[10] "MCCAMEY HOSPITAL"
[11] "FISHER COUNTY HOSPITAL DISTRICT"
[12] "HANSFORD COUNTY HOSPITAL"
[13] "ST LUKES LAKESIDE HOSPITAL"
Code
best <- function(state, outcome) {
file <- read.csv("outcome-of-care-measures.csv")
data <- file[file$State == state, ]
if (outcome == "heart attack") {
number <- 15 #column number
} else if (outcome == "heart failure") {
number <- 21
} else if (outcome == "pneumonia") {
number <- 27
}
col <- as.numeric(data[, number]) #column data
lowest <- min(col, na.rm = TRUE) #lowest number
data$Hospital.Name[data[, number] == lowest] #result
}
Sources
PDF with instructions Check point 2.