0

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

Data I work with

PDF with instructions Check point 2.

  • If something is unclear please ask for more precise explanation. – Albert Gawin Jul 24 '20 at 15:14
  • 2
    Welcome to Stack Overflow! You may want to look at [How to make a great R reproducible example](https://stackoverflow.com/q/5963269/8386140); using some of the suggestions there will make it easier for others to help you. In particular, most of us on this site (myself included) aren't too keen to download data from links. Instead the preferred way is to [edit] your question to include the output of the R command `dput(data)`. If `data` is particularly large, you may want to do `dput(head(data, n))` for some `n` to make it more managable – duckmayr Jul 24 '20 at 15:21
  • Your function get 1 as min but the hospital you want has value of 106, are you sure you are correct? – Duck Jul 24 '20 at 15:52
  • @Duck I don't understand your question. Can you ask it in different way? Thanks for your time. – Albert Gawin Jul 24 '20 at 17:13
  • 1
    Instead of `lowest <- min(...); ... == lowest`, use `data[which.min(data[,number]),"Hospital.Name"]`. – r2evans Jul 24 '20 at 17:18
  • 1
    You also have a latent bug: if `outcome` is not one of those three, then your function will fail with `object 'number' not found`. Consider first using `outcome <- match.arg(outcome, c("heart attack", "heart failure", "pneumonia"))`; when it fails, its error message is clearer. – r2evans Jul 24 '20 at 17:21
  • Thank you for all of your comments. – Albert Gawin Jul 24 '20 at 23:57

1 Answers1

0

I'm gonna public solution, after hour of searching I found it! In firs steps I accidentally write wrong column numbers from documentation.

Column numbers are incorrect.


Solution

Simply change wrong column numbers (15, 21, 27) to (11, 17, 23)


Thanks

Thank you for your answers, it increased my knowledge. Have a nice weekend.