Hasan Jamil
Hasan Jamil

Reputation: 37

'list' object cannot be coerced to type 'double'

I am creating a function that read a hospital's dataset for the USA, gets two arguments the state and the outcome(heart attack for example), and return the name of the hospital that has the lowest death in the case of that prespecified outcome so my function was.

best <- function(state, outcome){
        outcomedata <- read.csv("outcome-of-care-measures.csv")
        outcomedata <- outcomedata[ , c(2,7,11,17,23)]
        outcomedata[,c(3,4,5)] <- as.numeric(outcomedata[ ,c(3,4,5)])
        colnames(outcomedata) <- c("Hospital", "State", "heartattack", "hf", "pneumonia")
        outcomedata$heartattack <- as.numeric(outcomedata$heartattack)
        outcomedata$hf <- as.numeric(outcomedata$hf)
        outcomedata$pneumonia <- as.numeric(outcomedata$pneumonia)
        outcomedata <- outcomedata[outcomedata$State == state, ]
        deathoutcomes <- c("heart attack", "heart failure", "pneumonia")
       
        if(!(state %in% outcomedata[['State']]) | !(outcome %in% deathoutcomes)){
            print("error")
        } else{
if(outcome == "heart attack"){
 lowestdeathheartattack <- outcomedata[outcomedata$heartattack == min(outcomedata$heartattack), ]
 lowestdeathheartattack <- lowestdeathheartattack[order(lowestdeathheartattack$Hospital), ]
 print(lowestdeathheartattack)
} else if(outcome == "heart failure"){
  lowestdeathheartf <- outcomedata[outcomedata$hf == min(outcomedata$hf), ]
  lowestdeathheartf <- lowestdeathheartf[order(lowestdeathheartf$Hospital), ]
  print(lowestdeathheartf)
} else if(outcome == "pneumonia"){
  lowestpneumoniadf <- outcomedata[outcomedata$pneumonia == min(outcomedata$pneumonia), ]
  lowestpneumoniadf <- lowestpneumoniadf[order(lowestpneumoniadf$Hospital), ]
  print(lowestpneumoniadf) }}}

I will get the error

best("NY", "heart failure")
Error in best("NY", "heart failure") : 
  'list' object cannot be coerced to type 'double'

I tried using lapply I got he same problem.

outcomedata[,c(3,4,5)] <- lapply(outcomedata[ ,c(3,4,5)], as.numeric)

Upvotes: 0

Views: 69

Answers (1)

jpsmith
jpsmith

Reputation: 17205

I believe this will work, and simplify your code. First, you should put all of the following data manipulation code outside of your function (I imported your original data as df):

outcomedata <- df[ , c(2,7,11,17,23)]
outcomedata[3:5] <- lapply(outcomedata[3:5], as.numeric)
colnames(outcomedata) <- c("Hospital", "State", "heartattack", "hf", "pneumonia")

Note all the other code was unnecessary, and your original error was resolved by outcomedata[3:5] <- lapply(outcomedata[3:5], as.numeric), though more problems persisted in your function.

You can run the following function to find the minimum death in the case of that prespecified outcome within a desired state:

dplyr

dplyr_function <- function(state_want, outcome){
  outcomedata %>% 
    filter(State == state_want) %>% 
    slice_min({{outcome}}) %>% 
    select(c(Hospital, State,{{outcome}}))
}

For instance, dplyr_function("GA", heartattack) returns:

#                  Hospital State heartattack
#1 STEPHENS COUNTY HOSPITAL    GA        12.9

Also note that something like this:

outcomedata %>% 
  group_by(State) %>% 
  slice_min(hf) %>% 
  select(c(Hospital, State,hf))

Will return a data set with the minimum of hf in every state. Not sure if this would be easier for your application.

Base R

In base R, you can use which.min():

baseRfunction <- function(state_want, outcome){
  a <- outcomedata[outcomedata$State == state_want,]
  a[which.min(a[,outcome]), c("Hospital","State",outcome)]
}

Where baseRfunction("GA", "heartattack") returns:

#                    Hospital State heartattack 
#931 STEPHENS COUNTY HOSPITAL    GA        12.9 

Note that in this function, both inputs are in quotations ("").

Upvotes: 1

Related Questions