[英]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
針對問題15、21、23、24(以4分為標准)
1-0
2-33.3
3-66.7
4-100
對於Q 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
}
}
我的問題:
我在代碼的第一行中使用了...函數來定義我需要從x1到x31的參數。 我的最終目標不是從1到31手動定義它們。請有人告訴我如何從x1到x31定義參數,而無需在那里手動編寫
如何將新分數保存在函數中,以便以后可以用於分析?
通常,您可以使用list(...)
來使用...
捕獲任意數量的參數。 在其他問題中查看更多內容。 但是,當您認為自己不知道將要提供多少個參數並且仍然希望能夠處理該參數時,通常這是最好的選擇。 在這種情況下,您知道應該有31個答案,所以...
不合適。 相反,您應該嘗試將答案存儲在長度為31的向量中,並將其作為參數提供。 下面的例子。 在這里,我將創建簡短的oneliners,以根據您制定的規則轉換每個答案組。 這利用了R的數學函數,我認為它比使用if
語句對所有函數都更干凈(更快)? 然后,我們僅將變換應用於每個答案集,並將其分配給輸出分數。 示例顯示了一些隨機答案1-3。
如果您擔心拼寫錯誤,我會使用assert_that
包含一些注釋的代碼來檢查錯誤。 您可以在每個score_
函數內部檢查答案是否在正確的范圍內,例如,問題22的答案不應具有值4。
對於最后一部分,您不需要在函數內包括分配。 只需確保它返回您想要的內容,然后在調用該函數時進行賦值,如下所示。
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軟件包 (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
和查找表:
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。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.