I've been following a tutorial regarding ordering output for R dataframes:
https://www.statmethods.net/management/sorting.html
The problem I'm having is that when I use order the way that is presented in the tutorial, the below code spits out object not found
. I don't understand why it can't order the dataframe but the print statements seem to be working fine.
The following is the code I'm using:
#hospital name is row 2
#state is row 7
#heart attack is row 11
#heart failure is row 17
#pneumonia is row 23
best <- function(state, outcome){
colNum <- -1
##Semi hard coded :(
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),]
}
Sample Output:
> 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
data <- rawRelevant[order(rawRelevant$Rate,rawRelevant$Hospital),]
That tutorial seems to use a lot of variables that aren't necessarily in data.frames. – MrFlick Jul 1 '14 at 2:35