EDIT again: SOLUTION: added a * to multiply and created a new project which helped with the console not even giving me outputs for the simplest of code.
EDIT: I am a beginner. I have no idea what I'm doing wrong. R is not creating the function in the environment when I run the code, so I guess that is why it is not working, but I do not know why. This is for a homework assignment, so I need to create a function and not use a cronbachs alpha function that already exists.
Based on my calculations on paper, my answer should be 0.54347826
# Create Data frame
data <- data.frame(item1 = c(0,0,0,1,0),
item2 = c(0,0,1,1,1),
item3 = c(0,1,1,1,1),
item4 = c(0,1,1,1,0),
item5 = c(0,1,0,0,1))
data
#Add new column that is sum of each row
data$total <- data$item1 + data$item2 + data$item3 + data$item4 + data$item5
#calculate item and total variance
var1<-var(data$item1)
var2<-var(data$item2)
var3<-var(data$item3)
var4<-var(data$item4)
var5<-var(data$item5)
var.t<-var(data$total)
# Create function to calculate cronbach's alpha
alpha.function <- function(v1,v2,v3,v4,v5,vt){
alpha <- (5/(5-1))(1-(v1+v2+v3+v4+v5)/(vt))
return(alpha)
}
alpha.function(var1, var2, var3, var4, var5, var.t)