如何使用for循环计算几种规模的可靠性

时间:2019-06-28 13:20:55

标签: r for-loop

我的数据集中包含多个比例。我需要计算所有这些量表的可靠性。我可以使用for循环来简化代码。 我的数据示例如下:

js1 js2 js3 js4 js5 js6 js7 js8 IRI1 IRI2 IRI3 IRI4 IRI5 IRI6 IRI7 IRI8 IRI9 IRI10 IRI11 IRI12 IRI13 IRI14         
 2   2   0   1   3   4   5   1   1    2    2    1    4    3    5    1     4    3     2     3     1     4
 3   0   4   5   0   3   2   1   5    5    4    2    3    1    5    2     3    3     2     1     2     5
 5   3   4   3   1   1   0   1   3    4    3    1    2    2    3    4     1    5     3     2     1     4


我的部分代码如下:

justSenNames <- c(paste("justsens",1:8,sep = '')) 
justSenKeys <- c(1,2,3,4,5,6,7,8)

justSenAlpha <-  psych::alpha(df4321[,justSenNames], keys=justSenKeys)  # calculate the alpha coefficient of JS
print(justSenAlpha$total)  # print the alpha for JS

#from the Interpersonal Reactivity Index
df4321[, paste0("IRI_cn_",c(2,3,7,8,9))] <- 6- df4321[,paste0("IRI_cn_",c(2,3,7,8,9))]
IRINames <- c(paste('IRI',1:14,sep = ''))   
#IRIKeys <- c(1,-2,-3,4,5,6,-7,-8,-9,10,11,12,13,14) # original reverse coding
IRIKeys <- c(1,2,3,4,5,6,7,8,9,10,11,12,13,14)       # reverse coded as negative

IRIAlpha <-  psych::alpha(df4321[,IRINames], keys=IRIKeys)  # calculate the alpha coefficient of IRI
print(IRIAlpha$total)  # print the alpha for IRI

我想用于JS和IRI的循环计算可靠性,我以这种方式尝试过:

ScalesNames <- c('justSenNames','IRINames')
KeyNames <- c ('justSenKeys','IRIKeys')
for (i in ScalesNames){
    for (j in KeyNames) {
    alpha <- psych::alpha(df4321[,i],keys = j)
    print(Alpha$total)
}
}
Error in `[.data.frame`(df4321, , i) : undefined columns selected

0 个答案:

没有答案