jeudi 25 juin 2015

Subsetting data frame in R, based on function character parameter

I need to choose a column in a large data set from CSV.

I have this function. Two character arguments - is name of State and desease name. Based on disease name I need to choose the right column in a large dataset to perform calculations.

best <- function(state, outcome) {

  OutcomeData <- read.csv("outcome-of-care-measures.csv", colClasses = "character")

Second, I try to assign to variable OutcomeCol a value, based on function parameter outcome. Number is a column number in OutcomeData dataset corresponding outcome parameter.

I 
  ## 2 - Hospital.Name
  ## 7 - State
  ## 11 - 30 Day mortality Heart Attack
  ## 17 - 30 Day mortality Heart Failure
  ## 23 - 30 Day mortality Pneumonia

  ## Choose Outcome for filtering data
  if (outcome == "heart attack") {OutcomeCol <- 11}
  if (outcome == "heart failure") {OutcomeCol <- 17}
  if (outcome == "pneumonia") {OutcomeCol<- 23}
  try(if (!exists("OutcomeCol")) {stop("invalid outcome")}, silent = TRUE)

But when I try to run function best("TX", "heart failure") nothing happens. If I try to place print(OutcomeCol) after if statements - it returns NA.

Where is the error? Maybe there another simply way to perform this action?

Aucun commentaire:

Enregistrer un commentaire