Search code examples
rfunctionmapply

Function for calculating GCS score using Mapply not returning scores from text entries


I have a tibble to calculate the glasgow coma scale with the following column names: "gcs_eye" "gcs_motor" "gcs_verbal" "gcs_total" The first three columns were made by a check box survey, so they are either empty or have a predictable character string. GCS total is either empty or a number. My goal is to create a new column that takes the number from the GCS total column if it is present, or uses the test entries from the other three columns to calculate the GCS.

To do that, I'm using the code below. However, this is only carrying over the gcs_total score and isn't calculating the GCS from the text (only dumping out NAs). Can someone help me figure out what I'm doing wrong? Also, can someone tell me when I need to us + at the end beginning of an indented line of code? Thank you! I've also put some sample data below.


##Calculate GCS
GCS <- function(total, eye, motor, verbal){
  #return GCS total if calculated previously
  if (is.numeric(total)) {
    return(total) 
  } else {
  #Set baseline GCS
  GCS <- NA
  }
  if (eye == "Spontaneous"){
    GCS <- 6
  } else if (eye == "To voice"){
    GCS <- 5
  } else if (eye == "To pain"){
    GCS <- 4
  } else if (eye == "None"){
    GCS <- 3 #None adds no points
  } else {
    GCS <- GCS 
  }
  if (verbal == "Oriented"){
    GCS <- GCS +4
  } else if (eye == "Confused"){
    GCS <- GCS +3
  } else if (eye == "Inappropriate"){
    GCS <- GCS +2
  } else if (eye == "Incomprehensible"){
    GCS <- GCS +1
  } else {
    GCS <- GCS #None adds no points
  }
  if (motor == "Obeys commands"){
    GCS <- GCS +5
  } else if (motor == "Localizes to pain"){
    GCS <- GCS +4
  } else if (motor == "Flexion withdrawal"){
    GCS <- GCS +3
  } else if (motor== "Abnormal flexion"){
    GCS <- GCS +2
  } else if (motor== "Abnormal extension"){
    GCS <- GCS +1
  } else {
    GCS <- GCS #None adds no points
  }
 return(GCS)
}
data$GCS <- mapply(FUN = GCS, total = data$gcs_total, eye = data$gcs_eye,
                   motor = data$gcs_motor, verbal = data$gcs_verbal)

Here is some sample data:

data$gcs_eye <- c("","","","Spontaneous","Spontaneous","To voice")
data$gcs_motor <- c("","","","Obeys commands","Localizes to pain"," 
Flexion withdrawal")
data$gcs_verbal <- c("","","","Confused","Incomprehensible","None")
data$gcs_verbal <- c(NA, 13, 15, 14, NA, NA)

(If I ran my current code, it should spit out c(NA, 13, 15, 14, NA, NA)


Solution

  • Will this work for you:

    If yes the you simply had a typo in eye == "Confused"...

    it should be Verbal == "Confused"... etc.

    I think @Gregor Thomas already addressed this in the comments:

    GCS <- function(total, eye, motor, verbal){
      #return GCS total if calculated previously
      if (is.numeric(total)) {
        return(total) 
      } else {
        #Set baseline GCS
        GCS <- NA
      }
      if (eye == "Spontaneous"){
        GCS <- 6
      } else if (eye == "To voice"){
        GCS <- 5
      } else if (eye == "To pain"){
        GCS <- 4
      } else if (eye == "None"){
        GCS <- 3 #None adds no points
      } else {
        GCS <- GCS 
      }
      if (verbal == "Oriented"){
        GCS <- GCS +4
      } else if (verbal == "Confused"){
        GCS <- GCS +3
      } else if (verbal == "Inappropriate"){
        GCS <- GCS +2
      } else if (verbal == "Incomprehensible"){
        GCS <- GCS +1
      } else {
        GCS <- GCS #None adds no points
      }
      if (motor == "Obeys commands"){
        GCS <- GCS +5
      } else if (motor == "Localizes to pain"){
        GCS <- GCS +4
      } else if (motor == "Flexion withdrawal"){
        GCS <- GCS +3
      } else if (motor== "Abnormal flexion"){
        GCS <- GCS +2
      } else if (motor== "Abnormal extension"){
        GCS <- GCS +1
      } else {
        GCS <- GCS #None adds no points
      }
      return(GCS)
    }