R x64 3.1.2 RStudio win8.1 64x error in submission assignment 3 best function *actually i have 2 code * *the first have good **output but error in submission :* *Selection: 1Error in best("BB", "heart attack") : Invalid stateCalled from: .rs.breakOnError(TRUE)* best <- function(state, outcome) { outmeasures <- read.csv("outcome-of-care-measures.csv", colClasses = "character") caremeasures <- subset(outmeasures,,select=c("Hospital.Name", "State" , "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")) names(caremeasures) <- c("Hospital.Name", "State", "Heart.Attack", "Heart.Failure", "Pneumonia") measuresstate <- subset(caremeasures[caremeasures$State == state , c("Hospital.Name", "State" , "Heart.Attack", "Heart.Failure", "Pneumonia")]) if(!state %in% measuresstate$State) { stop("Invalid state") } if(outcome == "heart attack") { minHos <- measuresstate[, c("Hospital.Name", "Heart.Attack")] minHos[, "Heart.Attack"] <- suppressWarnings(as.numeric(minHos[, "Heart.Attack"])) minHosN <- na.omit(minHos) minHospital <- minHosN[with(minHosN, order(Heart.Attack, Hospital.Name)), ] return(minHospital[[1]][[1]]) }else if(outcome == "heart failure") { minHos <- measuresstate[, c("Hospital.Name", "Heart.Failure")] minHos[, "Heart.Failure"] <- suppressWarnings(as.numeric(minHos[, "Heart.Failure"])) minHosN <- na.omit(minHos) minHospital <- minHosN[with(minHosN, order(Heart.Failure, Hospital.Name)), ] return(minHospital[[1]][[1]]) }else if(outcome == "pneumonia") { minHos <- measuresstate[, c("Hospital.Name", "Pneumonia")] minHos[, "Pneumonia"] <- suppressWarnings(as.numeric(minHos[, "Pneumonia"])) minHosN <- na.omit(minHos) minHospital <- minHosN[with(minHosN, order(Pneumonia, Hospital.Name)), ] return(minHospital[[1]][[1]]) }else{ stop("Invalid outcome") } }
*the second have error* (best("BB", "heart attack") Error in minHospital[[1]][[1]] : subscript out of bounds) *and it's looks like no check on state the is as follow :* best <- function(state, outcome) { outmeasures <- read.csv("outcome-of-care-measures.csv", stringsAsFactors=FALSE, na.strings="Not Available") if(!state %in% outmeasures$State | !outcome %in% outmeasures$Hospital.30.Day.Death..Mortality..Rates.from.Heart.Attack | !outcome %in% outmeasures$Hospital.30.Day.Death..Mortality..Rates.from.Heart.Failure | !outcome %in% outmeasures$Hospital.30.Day.Death..Mortality..Rates.from.Pneumonia) { caremeasures <- subset(outmeasures,,select=c("Hospital.Name", "State" , "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")) names(caremeasures) <- c("Hospital.Name", "State", "Heart.Attack", "Heart.Failure", "Pneumonia") measuresstate <- subset(caremeasures[caremeasures$State == state , c("Hospital.Name", "State" , "Heart.Attack", "Heart.Failure", "Pneumonia")]) if(outcome == "heart attack") { minHos <- measuresstate[, c("Hospital.Name", "Heart.Attack")] minHos[, "Heart.Attack"] <- suppressWarnings(as.numeric(minHos[, "Heart.Attack"])) minHosN <- na.omit(minHos) minHospital <- minHosN[with(minHosN, order(Heart.Attack, Hospital.Name)), ] return(minHospital[[1]][[1]]) }else if(outcome == "heart failure") { minHos <- measuresstate[, c("Hospital.Name", "Heart.Failure")] minHos[, "Heart.Failure"] <- suppressWarnings(as.numeric(minHos[, "Heart.Failure"])) minHosN <- na.omit(minHos) minHospital <- minHosN[with(minHosN, order(Heart.Failure, Hospital.Name)), ] return(minHospital[[1]][[1]]) }else if(outcome == "pneumonia") { minHos <- measuresstate[, c("Hospital.Name", "Pneumonia")] minHos[, "Pneumonia"] <- suppressWarnings(as.numeric(minHos[, "Pneumonia"])) minHosN <- na.omit(minHos) minHospital <- minHosN[with(minHosN, order(Pneumonia, Hospital.Name)), ] return(minHospital[[1]][[1]]) }else{ stop("Invalid outcome") } } stop("Invalid State") } what is the problem I'm really confused and not understand what is the problem i need to put my hand on the problem Sorry for inconvenience [[alternative HTML version deleted]] ______________________________________________ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.