0

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)
}

1 Answers1

0

Store the data in a list :

a.1.1 <- vector('list', 8)
a.1.2 <- vector('list', 8)
a.1.3 <- vector('list', 8)

for(i in 1:8) {
  a.1.1[[i]] <- cohen.d((df$score[df$Group==1 & df$indicator==i]), 
                        (df$score[df$Group==2 & df$indicator==i]),
                         pooled=T, hedges.correction = T, na.rm=T)
  
  a.1.2[[i]]<-cohen.d((df$score[df$Group==2 & df$indicator==i]), 
                     (df$score[df$Group==3 & df$indicator==i]),
                      pooled=T, hedges.correction = T, na.rm=T)
  
  a.1.3[[i]] <- cohen.d((df$score[df$Group==1 & df$indicator==i]), 
                      (df$score[df$Group==3 & df$indicator==i]),
                       pooled=T, hedges.correction = T, na.rm=T)
}
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
  • Thank you! Could you please share when to use single, double, no brackets? – R_is_Broken Sep 06 '20 at 05:02
  • It depends on what you want to do. If you store data in a list (like in this example ) you need to use double bracket. To store data in a vector you can use single bracket. The same goes for extracting data as well. See this post for detailed difference between them https://stackoverflow.com/questions/1169456/the-difference-between-bracket-and-double-bracket-for-accessing-the-el?rq=1 – Ronak Shah Sep 06 '20 at 05:08