I am attempting to write a for loop in R, using the index value to subset the data, but I get errors that my brackets are unexpected. When I remove them from the body of the code, I get an error that the object I am creating cannot be found(yes! It shouldn't be found... the loop is supposed to create it). I am familiar with loops, but less experienced at writing them in R. To be clear, I do not understand when to use the single brackets [i] or the double [[i]]. I simply want my loop to run successfully and generate 24 runs of the effect size calculation.
I have tried numerous combinations of [i],[[i]],and i. I have also changed the variable type on indicator, to no avail.
Thank you for any help!
My current code is as follows:
Dataset Example:
structure(list(Group = c(1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2,
2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3), id = c("Person1", "Person1",
"Person1", "Person1", "Person1", "Person1", "Person1", "Person1",
"Person2", "Person2", "Person2", "Person2", "Person2", "Person2",
"Person2", "Person2", "Person3", "Person3", "Person3", "Person3",
"Person3", "Person3", "Person3", "Person3"), year = c(2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2
), indicator = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 1L, 2L, 3L,
4L, 5L, 6L, 7L, 8L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L), score = c(3.5,
3.5, 2, 3, 3.5, 4, 3, 4, 2.25, 2.5, 1.75, 1.5, 2, 2.75, 2, 2.75,
1.75, 2, 1.75, 2, 2, 2.5, 2, 2.5)), class = c("tbl_df", "tbl",
"data.frame"), row.names = c(NA, -24L))
Code in current form:
library(effsize)
for(i in 1:8){
a.1.1.[i]<-cohen.d((df[df$Group==1 & df$indicator==[i],]$score), (df[df$Group==2 & df$indicator==[i],]$score),pooled=T, hedges.correction = T, na.rm=T)
a.1.2.[i]<-cohen.d((df[df$Group==2 & df$indicator==[i],]$score), (df[df$Group==3 & df$indicator==[i],]$score),pooled=T, hedges.correction = T, na.rm=T)
a.1.3.[i]<-cohen.d((df[df$Group==1 & df$indicator==[i],]$score), (df[df$Group==3 & df$indicator==[i],]$score),pooled=T, hedges.correction = T, na.rm=T)
}