One of projects

rankhospital <- function(state, outcome, num = "best") {

    outc <- read.csv("outcome-of-care-measures.csv", header = TRUE, colClasses = "character")
    dis <- c("heart attack", "heart failure", "pneumonia")
    print(paste(" [[[[ ", num))
    ## Check that state and outcome are valid

    orsta <- names(table(outc$State))
    library(plyr)
    if (state %in% orsta) {
        print("Good state")

        if (outcome %in% dis) {
            newsta <- subset(outc, outc$State == state)
            newsta <- data.frame(newsta)
            print("PPPP ")
            if (outcome == "heart attack") {
                print("1")
                newsta[, 11] <- as.numeric(newsta[, 11])
                print("2")
                newsta <- newsta[complete.cases(newsta[, 11]), ]
                print("3")
                newsta <- arrange(newsta, (newsta[, 11]), newsta[, 2])
                print(paste("LLLLLLLL  "))
            }
            if (outcome == "heart failure") {
                print("4")
                newsta[, 17] <- as.numeric(newsta[, 17])
                print("5")
                # print(newsta[,17])
                newsta <- newsta[complete.cases(newsta[, 17]), ]

                print("6")
                newsta <- arrange(newsta, (newsta[, 17]), newsta[, 2])

                print(paste("MMM "))
            }
            if (outcome == "pneumonia") {
                print("7")
                newsta[, 17] <- as.numeric(newsta[, 23])
                print("8")
                newsta <- newsta[complete.cases(newsta[, 23]), ]
                print("9")
                newsta <- arrange(newsta, (newsta[, 23]), newsta[, 2])
                print(paste("NNN  "))
            }
            if (num == "worst") {
                print("in best")
                print(newsta[1, 2])
                print("PPPP")
                t <- nrow(newsta)
            }
            if (num == "best") {
                print("in worst")
                print(newsta[nrow(newsta), 2])
                t <- 1
            }
            if (is.numeric(num) == TRUE) {
                # this line checks whether a variable  is numeric or not
                print("in else")
                print(num)
                num <- as.integer(num)
                if (num <= nrow(newsta)) {
                  t <- num
                }
                if (num > nrow(newsta)) {
                  sol <- "NA"
                  print(sol)
                  print("   ::::: ")
                  t <- 1
                  print(t)
                  ## stop('invalid outcome')
                }
                print("oooo")
                print(t)
                print("{{{{}}}")
            }
            print(":::")
            sol <- (newsta[t, 2])
            print(sol)
        } else {
            t <- 1
            print(" ****")
            sol <- "NA"
            print(sol)
        }


    } else {
        print(" %%%")
        sol <- "NA"
        t <- 1
        print(sol)
    }
    print(ifelse(sol %in% orsta, sol, NA))
    print("  ++++ ")
    return(ifelse(sol %in% orsta, sol, NA))

}