Order in R: object not found

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:

#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),]
}

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 
+4
source share
1 answer

R , , , . "[", "$":

data <- rawRelevant[ order( rawRelevant$Rate, rawRelevant$Hospital), ]

, "", , R-. , , , someones attach rawRelevant, . attach , , , .

, UCLA attach(hsb2). UCLA R, SAS SPSS. , , , , " ".

+13

Source: https://habr.com/ru/post/1546642/


All Articles