# [R] Code for Coefficent (Cronbach's) Alpha

Magill, Brett MagillB at usa.redcross.org
Mon Jul 17 21:13:22 CEST 2000

```Hi all,

I am trying to teach myself to use and program R (How else do you do it?
lol)  Anyway, I wrote a piece of code to compute coefficent alpha for a
scale.  As I am neither a statistician nor a programmer, I wanted people's
feedback.  The code appears to work (Win 95 with R 1.0.1) and I have
verified my result with SPSS and it was correct (much to my astonishment!).

Nonetheless, I am sure there are better ways to do things than I did it as I
have limited documentation and was reading as I programmed.  Much of the
process was trial and error for me (oops, error!, how can I fix that?).  So,
as an opportunity to learn, I thought I would send the code to the list for
comments.  Would be interested in hearing if there are potential problems
with my code or easier ways of doing things.  Please be nice, I know much of
the "programming" here is probably naive.

Best,

Brett

reliability.alpha<-function(x){

score.total<-0
var.items<-0
var.total<-0
z.x<-0
z.score.total<-0
z.var.items<-0
z.x<-scale(x, center = TRUE, scale = TRUE)
n.items<-length(x)

for (i in 1:n.items) score.total<-score.total + x[,i]
for (i in 1:n.items) var.items<-var.items + var(x[,i])
for (i in 1:n.items) z.score.total<-z.score.total + z.x[,i]
for (i in 1:n.items) z.var.items<-z.var.items + var(z.x[,i])

var.total<-var(score.total)
z.var.total<-var(z.score.total)

cronbachs.alpha<-(n.items/(n.items-1))*((var.total -
var.items)/var.total)
z.cronbachs.alpha<-(n.items/(n.items-1))*((z.var.total -
z.var.items)/z.var.total)

return(cronbachs.alpha, z.cronbachs.alpha)}
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._

```