R 中的嵌套 For 和 If 循环

Nested For and If Loops in R

我正在尝试用 R 编写用于临床测试的函数代码。我的 R 技能很生疏,如果能得到任何帮助,我将不胜感激。

我正在尝试编写的函数需要 31 个值(患者填写的临床测试中有 31 个问题)。然后分别对这 31 个值进行评分(大多数问题具有不同的范围),然后组合在一起以获得不同参数的加权平均值。

得分范围:

对于 Q 1(定义为 x1)- 将响应乘以 10

对于 Q 2、6、5、9 -(评分为 6)将它们评分为
1 - 100
2 - 80
3 - 60
4 - 40
5 - 20
6 - 0。

Q 3,4,7,8,10,11,12,13,16,17,18(评分为 6)
1 - 0
2 - 20
3 - 40
4 - 60
5 - 80
6 - 100

Q 14、25、26、27、28、29、30(评分为 5)
1 - 100
2 - 75
3 - 50
4 - 25
5 - 0

Q 19,20(评分为 5)
1 - 0
2 - 25
3 - 50
4 - 75
5 - 100

Q 15、21、23、24(评分为 4)
1 - 0
2 - 33.3
3 - 66.7
4 - 100

第 22 期
1 - 0
2 - 50
3 -100

qolie31 <- function(x1, x2, x3, ...){
  x1a <- x1*10 
  z <- c(x2, x5, x6, x9)  
  {for (i in z){
    if (i==1){x == 100}
    else if(i==2){x == 80}
    else if(i==3){x==60}
    else if(i==4){x==40}
    else if(i==5){x==20}
    else (i==6){x==0}
    z2 <- x
  }
}

我的问题:

  1. 我在第一行代码中使用了 ... 函数来定义我需要从 x1 到 x31 的参数。我的最终目标不是从 1 到 31 手动定义它们。请有人告诉我如何定义从 x1 到 x31 的参数,而无需手动在那里写

  2. 如何在函数中保存新的分数,以便以后分析时使用?

通常,您可以使用 list(...) 捕获任意数量的参数 ...。在 this other question 中查看更多内容。然而,当您认为您不知道将提供多少参数并且无论如何都希望能够处理这些参数时,这通常是最好的。在这种情况下,您知道应该有 31 个答案,因此 ... 不合适。相反,您应该尝试将答案存储在长度为 31 的向量中并将其作为参数提供。下面的例子。在这里,我创建了简短的单行代码,以根据您制定的规则转换每个答案组。这利用了 R 的数学函数,我认为它比对所有内容使用 if 语句更干净(更快?)。然后我们只需将转换应用于每组答案并将它们分配给输出分数。显示了一些随机答案 1-3 的示例。

如果您担心拼写错误是个问题,我包含了一些使用 assert_that 来检查错误的注释代码。您可以在每个 score_ 函数内部检查答案是否在正确的范围内,例如,问题 22 的答案不应具有值 4.

对于最后一部分,您不需要在函数内包含赋值。只要确保它 returns 是你想要的,然后在调用函数时进行赋值,如下所示。

eg_ans <- sample.int(3, 31, replace = TRUE)

transform_scores <- function(answers){
  # assertthat::assert_that(
  #   length(answers) == 31,
  #   msg = "There are not 31 values in input vector"
  # )
  score1 <- function(ans) ans * 10
  score6a <- function(ans) (6 - ans) * 20
  score6b <- function(ans) (ans - 1) * 20
  score5a <- function(ans) (5 - ans) * 25
  score5b <- function(ans) (ans - 1) * 25
  score4 <- function(ans) (ans - 1) * (100 / 3)
  score3 <- function(ans) (ans - 1) * 50

  scores <- numeric(31)
  scores[1] <- score1(answers[1])
  scores[c(2, 5:6, 9)] <- score6a(answers[c(2, 5:6, 9)])
  scores[c(3:4, 7:8, 10:13, 16:18)] <- score6b(answers[c(3:4, 7:8, 10:13, 16:18)])
  scores[c(14, 25:30)] <- score5a(answers[c(14, 25:30)])
  scores[19:20] <- score5b(answers[19:20])
  scores[c(15, 21, 23:24)] <- score4(answers[c(15, 21, 23:24)])
  scores[22] <- score3(answers[22])
  return(scores)
}

eg_scores <- transform_scores(eg_ans)
eg_scores
#>  [1]  30.00000  60.00000   0.00000  20.00000 100.00000 100.00000   0.00000
#>  [8]  20.00000  60.00000  20.00000   0.00000  40.00000   0.00000  75.00000
#> [15]  66.66667   0.00000   0.00000  20.00000  50.00000  50.00000  66.66667
#> [22] 100.00000   0.00000  33.33333 100.00000  75.00000 100.00000 100.00000
#> [29] 100.00000  50.00000   0.00000

reprex package (v0.2.0) 创建于 2018-04-24。

您可以使用 plyr 包中的 mapvalues 函数。

    rescaleq<- function(x){
    require(plyr)
    if (length(x) != 30) stop("Vector of 30 elements required")
    x[1]<- x[1]*10
    x[c(2, 5, 6, 9)]<- mapvalues(x[c(2, 5, 6, 9)], from = 1:6, to = seq(100, 0, by = -20))
    x[c(3,4,7,8,10,11,12,13,16,17,18)]<- mapvalues(x[c(3,4,7,8,10,11,12,13,16,17,18)], from  = 1:6, to = seq(0, 100, by = 20))
    x[c(14, 25, 26, 27, 28, 29, 30)]<- mapvalues(x[c(14, 25, 26, 27, 28, 29, 30)], from = 1:5, to = seq(100, 0, by = -25))
    x[c(19, 20)]<- mapvalues(x[c(19, 20)], from = 1:5, to = seq(0, 100, by = 25))
    x[c(5, 21, 23, 24)]<- mapvalues(x[c(5, 21, 23, 24)], from = 1:4, to = seq(0, 100, length.out = 4))
     x[22]<- mapvalues(x[22], from = 1:3, to = seq(0, 100, by = 50))
    return(round(x, 2))
}

并用一些数据对其进行测试:

> xvector <- sample.int(3, 31, replace=T)
> xvector
# [1] 2 1 3 2 2 3 2 1 1 3 1 3 1 1 1 1 2 1 3 1 1 2 1 1 2 2 3 1 3 3 
> rescaleq(xvector[-31]) # Note that below, these are messages NOT errors or warnings
#The following `from` values were not present in `x`: 4, 5, 6
#The following `from` values were not present in `x`: 4, 5, 6
#The following `from` values were not present in `x`: 4, 5
#The following `from` values were not present in `x`: 2, 4, 5
#The following `from` values were not present in `x`: 3, 4
#The following `from` values were not present in `x`: 1, 3
# [1]  20.00 100.00  80.00  60.00 100.00  40.00  20.00  20.00   0.00  40.00   0.00  40.00
#[13]   0.00   0.00  20.00   0.00 100.00  75.00  75.00  50.00 100.00  50.00  50.00  50.00
#[25]   0.00  33.33   0.00   0.00   0.00  50.00

如果您想删除由 mapvalues 生成的消息,请尝试将它们包裹 suppressMessages,即 suppressMessages(mapvalues(x[c(2, 5, 6, 9)], from = 1:6, to = seq(100, 0, by = -20)))

另一种方式,这次使用 tidyverse 和查找 table:

library(tidyverse)

data = "
1                             | 10
2,6,5,9                       | 100,80,60,40,20,0
3,4,7,8,10,11,12,13,16,17,18  | 0,20,40,60,80,100
14, 25, 26, 27, 28, 29, 30    | 100,75,50,25,0
19,20                         | 0,25,59,75,100
15, 21, 23, 24                | 0, 33.3, 66.7, 100
22                            | 0,50,100
"

df <- read.table(text = data, sep = '|', 
                 stringsAsFactors = F, 
                 col.names = c('q', 'factor'),
                 strip.white = T)

# create the lookup table
# save it somewhere
# as we only need to generate it once
lookup <- df %>%
  separate_rows(q, sep = ',') %>%
  separate_rows(factor, sep = ',', convert = T) %>%
  group_by(q) %>%
  mutate(item = 1:n()) %>%
  ungroup()

# calculate the score
calc_score <- function(x) {
  score <- 0
  for (i in seq_along(x)) {
    f <- lookup %>% filter(q == i, item == x[i]) %>% select(factor) %>% pull()
    score <- score + i * f
  }
  score
}

v <- c(1,4,3)
(score <- calc_score(v))

此示例的得分为 210。