0

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!

Ross
  • 5
  • 4
  • Hi Ross, welcome to StackOverflow. It would greatly help if you made your question reproducible. A reproducible question is a question, which provides all the elements a potential helper would need to provide adequate help. You have provided your code and that's great. However, you also need to inlcude the necessary packages needed for the code to run. You mentioned it in your post, but it's even better to add `library(apaTable)` at the top of your code. Also, your loop contains `FinalDF` and we do not know what this data frame looks like. You could also include a short code that will mimic it – SavedByJESUS Jul 29 '20 at 23:26
  • Here is a question that was posted on StackOverflow in which the OP provided sample data. I'm giving it to you as an example: https://stackoverflow.com/questions/4227223/convert-a-list-to-a-data-frame/50595348#50595348 I also suggest you take a look at this short article here about reproducible examples (in R): https://community.rstudio.com/t/faq-whats-a-reproducible-example-reprex-and-how-do-i-do-one/5219 – SavedByJESUS Jul 29 '20 at 23:29
  • 2
    `paste` is rather vectorized. Can you describe what you are trying to accomplish? There is definitely a better way to write this. Probably maybe even without the for-loops – Onyambu Jul 29 '20 at 23:29
  • Thanks for the responses @SavedByJESUS. I have edited the post in response to your comments. – Ross Jul 30 '20 at 00:07

1 Answers1

0

The simple tables you are after can easily be generated as shown below. You could then apply whatever apaTable function using the tables in e.g. an apply family function.

library(dplyr)

v1 <- paste0("Col", 1:4)
v2 <- paste0("Col", 6:10)

v <- expand.grid(v1, v2, stringsAsFactors = FALSE) %>%
  arrange(Var1) %>%
  mutate(model = paste0(Var1, " ~ ", Var2))

With this output:

> v
   Var1  Var2        model
1  Col1  Col6  Col1 ~ Col6
2  Col1  Col7  Col1 ~ Col7
3  Col1  Col8  Col1 ~ Col8
4  Col1  Col9  Col1 ~ Col9
5  Col1 Col10 Col1 ~ Col10
6  Col2  Col6  Col2 ~ Col6
7  Col2  Col7  Col2 ~ Col7
8  Col2  Col8  Col2 ~ Col8
9  Col2  Col9  Col2 ~ Col9
10 Col2 Col10 Col2 ~ Col10
11 Col3  Col6  Col3 ~ Col6
12 Col3  Col7  Col3 ~ Col7
13 Col3  Col8  Col3 ~ Col8
14 Col3  Col9  Col3 ~ Col9
15 Col3 Col10 Col3 ~ Col10
16 Col4  Col6  Col4 ~ Col6
17 Col4  Col7  Col4 ~ Col7
18 Col4  Col8  Col4 ~ Col8
19 Col4  Col9  Col4 ~ Col9
20 Col4 Col10 Col4 ~ Col10

Is this helpful?

UPDATE

data(mtcars)
v1 <- colnames(mtcars)[1:4]
v2 <- colnames(mtcars)[6:10]

v <- expand.grid(v1, v2, stringsAsFactors = FALSE) %>%
  arrange(Var1) %>%
  mutate(model = paste0(Var1, " ~ ", Var2))

with the result below. You can do this for your data object as well.

> v
   Var1 Var2       model
1    am   wt     am ~ wt
2    am qsec   am ~ qsec
3    am   vs     am ~ vs
4    am   am     am ~ am
5    am gear   am ~ gear
6  gear   wt   gear ~ wt
7  gear qsec gear ~ qsec
8  gear   vs   gear ~ vs
9  gear   am   gear ~ am
10 gear gear gear ~ gear
11 qsec   wt   qsec ~ wt
12 qsec qsec qsec ~ qsec
13 qsec   vs   qsec ~ vs
14 qsec   am   qsec ~ am
15 qsec gear qsec ~ gear
16   vs   wt     vs ~ wt
17   vs qsec   vs ~ qsec
18   vs   vs     vs ~ vs
19   vs   am     vs ~ am
20   vs gear   vs ~ gear
21   wt   wt     wt ~ wt
22   wt qsec   wt ~ qsec
23   wt   vs     wt ~ vs
24   wt   am     wt ~ am
25   wt gear   wt ~ gear
Paul van Oppen
  • 1,443
  • 1
  • 9
  • 18
  • Hi Paul, I just edited my question, since I guess I am still not clear about what I need. The Col1, Col2, etc. are not the actual names, but could I do something similar with calling the colnames() from the DF? – Ross Jul 30 '20 at 22:02