使用Mapply计算GCS得分的函数未返回文本条目的得分。

huangapple go评论84阅读模式
英文:

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

问题

我有一个tibble来计算格拉斯哥昏迷评分,具有以下列名: "gcs_eye"、"gcs_motor"、"gcs_verbal"、"gcs_total"。前三列是通过复选框调查创建的,因此它们要么为空,要么包含可预测的字符串。GCS总分要么为空,要么是一个数字。我的目标是创建一个新列,如果GCS总分列中有数字,就取该数字,否则使用其他三列的测试输入来计算GCS。

为了实现这一目标,我使用以下代码。然而,这只传递了gcs_total分数,而没有计算文本中的GCS(只输出NAs)。有人能帮助我找出问题在哪吗?另外,有人能告诉我什么时候需要在缩进的代码行的开头或结尾使用+吗?

谢谢!我还在下面放了一些示例数据。

## 计算GCS
GCS <- function(total, eye, motor, verbal) {
  # 如果之前已经计算过GCS总分,则返回GCS总分
  if (is.numeric(total)) {
    return(total) 
  } else {
    # 设置基线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" 不添加分数
  } 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" 不添加分数
  }
  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" 不添加分数
  }
 return(GCS)
}
data$GCS <- mapply(FUN = GCS, total = data$gcs_total, eye = data$gcs_eye,
                   motor = data$gcs_motor, verbal = data$gcs_verbal)

这是一些示例数据:

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_total <- c(NA, 13, 15, 14, NA, NA)

(如果我运行我的当前代码,它应该输出c(NA, 13, 15, 14, NA, NA))

英文:

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 &lt;- function(total, eye, motor, verbal){
  #return GCS total if calculated previously
  if (is.numeric(total)) {
    return(total) 
  } else {
  #Set baseline GCS
  GCS &lt;- NA
  }
  if (eye == &quot;Spontaneous&quot;){
    GCS &lt;- 6
  } else if (eye == &quot;To voice&quot;){
    GCS &lt;- 5
  } else if (eye == &quot;To pain&quot;){
    GCS &lt;- 4
  } else if (eye == &quot;None&quot;){
    GCS &lt;- 3 #None adds no points
  } else {
    GCS &lt;- GCS 
  }
  if (verbal == &quot;Oriented&quot;){
    GCS &lt;- GCS +4
  } else if (eye == &quot;Confused&quot;){
    GCS &lt;- GCS +3
  } else if (eye == &quot;Inappropriate&quot;){
    GCS &lt;- GCS +2
  } else if (eye == &quot;Incomprehensible&quot;){
    GCS &lt;- GCS +1
  } else {
    GCS &lt;- GCS #None adds no points
  }
  if (motor == &quot;Obeys commands&quot;){
    GCS &lt;- GCS +5
  } else if (motor == &quot;Localizes to pain&quot;){
    GCS &lt;- GCS +4
  } else if (motor == &quot;Flexion withdrawal&quot;){
    GCS &lt;- GCS +3
  } else if (motor== &quot;Abnormal flexion&quot;){
    GCS &lt;- GCS +2
  } else if (motor== &quot;Abnormal extension&quot;){
    GCS &lt;- GCS +1
  } else {
    GCS &lt;- GCS #None adds no points
  }
 return(GCS)
}
data$GCS &lt;- 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 &lt;- c(&quot;&quot;,&quot;&quot;,&quot;&quot;,&quot;Spontaneous&quot;,&quot;Spontaneous&quot;,&quot;To voice&quot;)
data$gcs_motor &lt;- c(&quot;&quot;,&quot;&quot;,&quot;&quot;,&quot;Obeys commands&quot;,&quot;Localizes to pain&quot;,&quot;	
Flexion withdrawal&quot;)
data$gcs_verbal &lt;- c(&quot;&quot;,&quot;&quot;,&quot;&quot;,&quot;Confused&quot;,&quot;Incomprehensible&quot;,&quot;None&quot;)
data$gcs_verbal &lt;- c(NA, 13, 15, 14, NA, NA)

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

答案1

得分: 1

这部分代码是不需要翻译的。

英文:

Will this work for you:

If yes the you simply had a typo in eye == &quot;Confused&quot;...

it should be Verbal == &quot;Confused&quot;... etc.

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

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

huangapple
  • 本文由 发表于 2023年4月10日 23:32:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/75978449.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定