I am following a tutorial on ordering output for R-frames:
https://www.statmethods.net/management/sorting.html
The problem I am facing is that when I use the order presented in the tutorial, the code below spits out object not found. I don’t understand why he cannot order the data framework, but the print instructions seem to work fine.
The following is the code I'm trying to create:
best <- function(state, outcome){
colNum <- -1
if(outcome == "heart attack"){
colNum <- 11
} else if(outcome == "heart failure"){
colNum <- 17
} else if(outcome == "pneumonia"){
colNum <- 23
} else {
stop("invalid outcome")
}
raw <- read.csv("outcome-of-care-measures.csv", colClasses = "character")
if(sum(raw$State == state) <= 0){
stop("invalid state")
}
rawRelevant <- raw[with(raw, raw[,colNum] != "Not Available" &
raw[,7] == state),c(2,colNum)]
rawRelevant[,2] <- as.numeric(rawRelevant[,2])
names(rawRelevant) <- c("Hospital", "Rate")
print(rawRelevant$Hospital)
print(rawRelevant$Rate)
data <- rawRelevant[order(Rate,Hospital),]
}
Output example
> trial <- best("AK", "heart attack")
[1] "PROVIDENCE ALASKA MEDICAL CENTER" "MAT-SU REGIONAL MEDICAL CENTER"
[3] "FAIRBANKS MEMORIAL HOSPITAL" "ALASKA REGIONAL HOSPITAL"
[5] "ALASKA NATIVE MEDICAL CENTER"
[1] 13.4 17.7 15.5 14.5 15.7
Error in order(Rate, Hospital) : object 'Rate' not found
source
share