Amos Epelman
Amos Epelman

Reputation: 41

Removing 'Not Available' from a .csv file in R

I am trying to remove 'Not Available' from a .csv file using R. I have already figured out how to narrow down my file and arrange the data but the Not availables are not being removed. I have tried using complete.cases, na.omit and lapply and none of them work. Any help on this would be appreciated.See below for the assignment:

"Write a function called best that take two arguments: the 2-character abbreviated name of a state and an outcome name. The function reads the outcome-of-care-measures.csv file and returns a character vector with the name of the hospital that has the best (i.e. lowest) 30-day mortality for the specified outcome in that state. The hospital name is the name provided in the Hospital.Name variable. The outcomes can be one of “heart attack”, “heart failure”, or “pneumonia”. Hospitals that do not have data on a particular outcome should be excluded from the set of hospitals when deciding the rankings"

best <- function(state, outcome)
{
  chart <- read.csv("outcome-of-care-measures.csv", colClasses = "character")
  
  if (!any(state == chart$State)){
    stop("invalid state")
  } else if(!outcome %in% c("heart attack" , "heart failure" , "pneumonia")){
    stop("invalid outcome")
  } else {
    keys <- c("heart attack" = 11, "heart failure" = 17, "pneumonia" = 23)
    outcomeKey <- keys[outcome]
  }
  
  dataByState <- split(chart, chart$State)
  outcomeState <- dataByState[[state]]
  outcomeStateOrder <- na.omit(arrange(outcomeState, desc(outcomeState[,outcomeKey]),Hospital.Name))
  goodData <- outcomeStateOrder[complete.cases(outcomeStateOrder) , ]
  View(goodData)
  
}

best("TX", "heart attack")

Upvotes: 0

Views: 190

Answers (1)

Amos Epelman
Amos Epelman

Reputation: 41

In this case because "not available" is not the same as NA, I used the subset function to remove rows where the value was equal to "not available"

  goodData <- subset(outcomeState, outcomeState[, outcomeKey] != "Not Available")

Upvotes: 0

Related Questions