I am trying to populate a named list with the results of an OLS in R. I tried
li = list()
for (i in 1:10)
li[["RunOne"]][i] = lm(y~x)
Here RunOne
is a random name that designates the fitting run one, y
and x
are some predefined vectors. This breaks and gives me the error
Warning message:
In l[["RunOne"]][1] = lm(y ~ x) :
number of items to replace is not a multiple of replacement length
Though I understand the error, but I don't know how to fix it.
There are two solutions (depending on exactly what you want to do).
Create a list, and add an lm
object to each element:
li = list()
for (i in 1:10)
li[[i]] = lm(y~x)
Have a list of lists:
li[["RunOne"]] = list()
for (i in 1:10)
li[["RunOne"]][[i]] = lm(y~x)
Typically, single brackets [ ]
are used for vectors and data frames, double brackets are used for lists.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With