Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sortring in R: Object not found

Tags:

sorting

r

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 
like image 525
Zigu Avatar asked Jul 01 '14 02:07

Zigu


People also ask

Why am I getting object not found in R?

This error usually occurs for one of two reasons: Reason 1: You are attempting to reference an object you have not created. Reason 2: You are running a chunk of code where the object has not been defined in that chunk.

How do you define an object in R?

In RStudio, typing Alt + - (push Alt at the same time as the - key) will write <- in a single keystroke. Here are a few rules as of how to name objects in R. Objects can be given any name such as x , current_temperature , or subject_id . You want your object names to be explicit and not too long.

What does object of type closure is not Subsettable mean?

What does object of type 'closure' is not subsettable mean? The error occurs when you try to use indexing on a function (is not a function but a reactive variable) with the name instead of value.


Video Answer


1 Answers

In R you cannot just pass the column names to the [.data.table function, even if they might be imagined to be obviously referring to columns within the data.table being extracted or re-ordered. You need to use either "[" or "$":

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

The use of non-specific object names like "data" is discouraged, especially so when they are also the names of R functions as are data or df. One situation that might arise is that higher up in someones code there might have been an attach call for rawRelevant, and that would have the side-effect of appearing to promote the column-names to objects. But attach causes a lot of confusion since it is not meant for programming, only for interactive use, and so its use is also discouraged.

Notice that the UCLA people used attach(hsb2). Several years ago the UCLA stats websites were advising against R in preference to SAS and SPSS. Now they seem to have come around, but I don't think they are really completely "with the program."

like image 85
IRTFM Avatar answered Oct 08 '22 13:10

IRTFM