There are several scales included in my data set. I need to calculate reliabilities for all these scales. Can I use a for loop to simplify my code. A example of my data can be as following:
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
Part of my code is as following:
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
I want to use for loop caculate reliabilities for JS and IRI, I tried it in such a way:
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
