本文介绍了用lm对象填充列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用R中OLS的结果填充命名列表.
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)
此处RunOne
是一个随机名称,用于指定拟合运行一次,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).
-
创建一个列表,并将
lm
对象添加到每个元素:
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.
这篇关于用lm对象填充列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!