Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R中的嵌套For和If循环_R_Loops_Nested Loops - Fatal编程技术网

R中的嵌套For和If循环

R中的嵌套For和If循环,r,loops,nested-loops,R,Loops,Nested Loops,我正试图为R中的临床测试编写一个函数代码。我的R技能已经相当生疏了,我真的非常感谢任何帮助 我试图编写的函数包含31个值(患者填写的临床测试中有31个问题)。然后分别对这31个值进行评分(大多数问题有不同的范围),然后组合在一起,得到不同参数的加权平均值 评分范围: 对于Q 1(定义为x1)-将响应乘以10 对于问题2,6,5,9-(按6分制评分)将其评分为 1-100 2-80 3-60 4-40 5-20 6 - 0. 对于问题3、4、7、8、10、11、12、13、16、17、18(以6

我正试图为R中的临床测试编写一个函数代码。我的R技能已经相当生疏了,我真的非常感谢任何帮助

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

评分范围:

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

对于问题2,6,5,9-(按6分制评分)将其评分为
1-100
2-80
3-60
4-40
5-20
6 - 0.

对于问题3、4、7、8、10、11、12、13、16、17、18(以6分制评分)
1-0
2-20
3-40
4-60
5-80
6-100

对于问题14、25、26、27、28、29、30(按5分制评分)
1-100
2-75
3-50
4-25
5-0

对于第19,20题(按5分制评分)
1-0
2-25
3-50
4-75
5-100

对于问题15、21、23、24(按4分制评分)
1-0
2-33.3
3-66.7
4-100

对于Q 22
1-0
2-50
3-100


qolie31通常,您可以使用
列表(…)
捕获任意数量的参数。更多信息请参阅。然而,当您认为不知道要提供多少个参数并且希望能够处理这些参数时,这通常是最好的。在这种情况下,您知道应该有31个答案,因此
不合适。相反,您应该尝试将答案存储在长度为31的向量中,并将其作为参数提供。下面的例子。在这里,我创建了简短的一行程序,以根据您制定的规则转换每个答案组。这利用了R的数学函数,我认为它比使用
if
语句更简洁(更快?)。然后我们将转换应用于每组答案,并将它们分配到输出分数。示例中显示了一些随机答案1-3

如果您担心打字错误会成为一个问题,我会使用
assert\u(断言)
包含一些注释代码,以检查错误。您可以在每个
score\函数中检查答案是否在正确的范围内,例如,问题22的答案不应具有值4

对于最后一部分,您不需要在函数中包含赋值。只要确保它返回您想要的,并在调用函数时执行赋值,如下所示


eg_ans您可以使用
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))
}

rescaleq另一种方法,这次使用
tidyverse
和查找表:

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))
库(tidyverse)
数据=”
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%
单独的_行(因子,sep=',,convert=T)%>%
分组依据(q)%>%
变异(项=1:n())%>%
解组()
#计算分数
计算分数%pull()

分数你应该重新考虑你的方法,也许只有一个向量
x
和不同的索引就足够了(例如
x[1]=2
)。然后,对不同的组使用
seq_-along()
和%
中的
%in%
。使用缩进将帮助您的代码更清晰-直观地显示for循环、每个if语句等中发生的情况。我已经编辑了您的问题,强烈建议您使代码匹配。(大多数编辑都会让你很容易做到这一点)。您还有一个额外的
{
,适当的缩进将帮助您捕获类似的内容。仅供参考,您不需要在
之前为
添加
{
,在(…){
之后添加
。如果格式更好,您的问题也会得到更多帮助。您可以单击“编辑”在下面的按钮,并可以将你的表格式化为没有所有空行的代码。使用<代码>标尺::ReCale(Q1,C(0100)< /代码>),以便将其放在一个函数中,你将分组所有:或者只做<代码> m=函数(q,Rev=0){D=标尺::ReCalee(q,c(0100));如果(Rev)Rev(d)否则d}
您缺少问题
31
。您好,非常感谢。我尝试了您的代码,但从第三个问题开始,我的答案与代码中的答案不匹配。我键入了您的代码,用于值x@Ar1229,好的,我修复了它,尽管它现在明显不那么优雅。我已经搞错了索引!抱歉!还有,在最初的问题你提到了31个值,但你没有提供关于31的具体说明。非常感谢!第31个问题是一个整体的存在状态,这里没有评分,但后来合并并加权。所以你的代码中的31可以更改为30。很抱歉,我忘记了。再次感谢你。嗨@CalumYou,非常感谢您的介绍和详细的解释。它像魔术一样发挥作用。再次非常感谢,我将继续回到解释中,以帮助更好地编写代码。谢谢@Jan。我也尝试了seq_along()方法。
> 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
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))