I am trying trying to create a loop that utilizes a function to produce a document in r using the apaTable library.
The data frame (FinalDF) is a number of scores. All are numerical on a 0-100 scale. The names are all unique and I am just referencing them by number here.
library(apaTable)
FinalDF <- data.frame(replicate(10,sample(0:100,2000,rep=TRUE)))
for (j in 1:4){
for (i in 6:10){
assign(paste(colnames(FinalDF)[j], colnames(FinalDF)[i], "lm.loop", sep = "."), lm(paste(colnames(FinalDF)[j], colnames(FinalDF)[i], sep = " ~ "), data = FinalDF))
}
apa.reg.table(paste(paste(colnames(FinalDF)[[j]], colnames(FinalDF)[[6]], "lm.loop", sep = "."), paste(colnames(FinalDF)[[j]], colnames(FinalDF)[[7]], "lm.loop", sep ="."),
paste(colnames(FinalDF)[[j]], colnames(FinalDF)[[8]], "lm.loop", sep = "."),
paste(colnames(FinalDF)[[j]], colnames(FinalDF)[[9]], "lm.loop", sep = "."),
paste(colnames(FinalDF)[[j]], colnames(FinalDF)[[10]], "lm.loop", sep = "."), sep = " , "), filename = paste(colnames(FinalDF)[j], "Test.doc"))
}
Unfortunately, I am getting "Error: $ operator is invalid for atomic vectors"
I am not sure how to use the paste() function to create the names of the models that will be used in the function. I tried string, character, removing the quotes, and anything else I could find. Nothing is working!
In the end, I am trying to create a regression table with multiple simple regressions on the same table. (The library does a good job of outputting the regression model.)
For example, one table would be -
- FinalDF[1] ~ FinalDF[6] -- REGRESSION RESULTS FORMATTED FROM apaTABLES
- FinalDF[1] ~ FinalDF[7] -- REGRESSION RESULTS FORMATTED FROM apaTABLES
- FinalDF[1] ~ FinalDF[8] -- REGRESSION RESULTS FORMATTED FROM apaTABLES
- FinalDF[1] ~ FinalDF[9] -- REGRESSION RESULTS FORMATTED FROM apaTABLES
- FinalDF[1] ~ FinalDF[10] -- REGRESSION RESULTS FORMATTED FROM apaTABLES
The next would be -
- FinalDF[2] ~ FinalDF[6] -- REGRESSION RESULTS FORMATTED FROM apaTABLES
- FinalDF[2] ~ FinalDF[7] -- REGRESSION RESULTS FORMATTED FROM apaTABLES
- FinalDF[2] ~ FinalDF[8] -- REGRESSION RESULTS FORMATTED FROM apaTABLES
- FinalDF[2] ~ FinalDF[9] -- REGRESSION RESULTS FORMATTED FROM apaTABLES
- FinalDF[2] ~ FinalDF[10] -- REGRESSION RESULTS FORMATTED FROM apaTABLES
This would be in APA formatting, which is why I am using the apaTable library.
Later, I am hoping to make multiple regressions on the same table, like this -
- FinalDF[1] ~ FinalDF[6] + FinalDF[7] + FinalDF[8] -- REGRESSION RESULTS FORMATTED FROM apaTABLES
- FinalDF[1] ~ FinalDF[6] + FinalDF[7] + FinalDF[8] + FinalDF[9] -- REGRESSION RESULTS FORMATTED FROM apaTABLES
- FinalDF[1] ~ FinalDF[6] + FinalDF[7] + FinalDF[8] + FinalDF[9] + FinalDF[10] -- REGRESSION RESULTS FORMATTED FROM apaTABLES
- FinalDF[1] ~ FinalDF[6] + FinalDF[7] + FinalDF[8] + FinalDF[9] + FinalDF[10] + FinalDF[6]*FinalDF[7] -- REGRESSION RESULTS FORMATTED FROM apaTABLES
- etc.
The problem is that the function does not seem to recognize the names of the new data frames I am creating when I input them through the paste() function.
Thanks!