I need to access variables from two different data sets in a simple loop (below). (I realize this requires the Negative and Positive vectors to be the same length…luckily, this will always be the case.)
Groups<-c("bdiControl","bdi")
Positive<-c("PA","Sad_PA","Amuse_PA","Happy_PA","Disgust_PA")
Negative<-c("NA","Sad_NA","Amuse_NA","Happy_NA","Disgust_NA")
for (g in Groups) {
for (i in Positive) {
if (sd(Groups[[g]]$Positive[[i]])<sdthresh | sd(Groups[[g]]$Negative[[i]]])<sdthresh){
cat('Standard deviation too low to run\ ',Positive[[i]],Negative[[i]],'\ comparison')
}
else{
corr<-cor(Groups[[g]]$Positive[[i]],Groups[[g]]$Negative[[i]],use="complete.obs")
print("The correlation between " Positive[[i]] " and " Negative[[i]] " was " corr "for " Groups[[g]])
}
}
}
Other references I have tried include g$i, Groups[g]$Positive[i], g$Positive[[i]], and similar permutations. I think I am spinning my wheels in a rut of problem solving. Help?! 🙂
There were many issues with this code. While it wasn’t entirely clear what the code was trying to do (you should ask your question more clearly), I believe this will do what you want:
For example, when I created random datasets (always provide a reproducible example!) with:
The output is: