如何解决从coeftest对象列表中提取列
| 是否存在可以从“ 0”对象中提取两列或更多列的函数?一次很容易实现一个“ 0”对象,但是我可以对一个列表做同样的事情(一个“ 2”循环除外)吗?> # meaningless data
> temp <- data.frame(a = rnorm(100,mean = 5),b = rnorm(100,mean = 1),+ c = 1:100)
> formulas <- list(a ~ b,a ~ c)
> models <- lapply(formulas,lm,data = temp)
> library(lmtest)
> cts <- lapply(models,coeftest)
> # easy to extract columns one object at a time
> cts[[1]][,1:2]
Estimate Std. Error
(Intercept) 5.0314196 0.1333705
b -0.1039264 0.0987044
> # but more difficult algorithmically
> # either one column
> lapply(cts,\"[[\",1)
[[1]]
[1] 5.03142
[[2]]
[1] 5.312007
> # or two
> lapply(cts,1:2)
Error in FUN(X[[1L]],...) : attempt to select more than one element
也许更根本的问题是,是否有办法将“ 0”对象的内容转换为数据框,这将允许我单独提取列,然后使用“ 5”。谢谢!
编辑:我想以第一列和第二列结尾一个矩阵(或数据帧)。
[[1]]
Estimate Std. Error
(Intercept) 5.0314196 0.1333705
b -0.1039264 0.0987044
[[2]]
Estimate Std. Error
(Intercept) 5.312007153 0.199485363
c -0.007378529 0.003429477
解决方法
在这种情况下,
[[
是错误的子集功能。请注意,当您在列表上使用lapply()
时,要使用的内容是列表的组成部分,使用list[[i]]
将获得的位,其中i
是第i个组成部分。
这样,您只需在lapply()
调用中将cts[[1]][,1:2]
的[,1:2]
位。由于[
的参数,它有点棘手,但with8ѭ可以轻松实现:
> lapply(cts,`[`,1:2)
[[1]]
Estimate Std. Error
(Intercept) 4.926679544 0.1549482
b -0.001967657 0.1062437
[[2]]
Estimate Std. Error
(Intercept) 4.849041327 0.204342067
c 0.001494454 0.003512972
注意ѭ18之前的ѭ17。这相当于ѭ19。
, 我不确定这是否是您想要的,但是如何:
> do.call(\"rbind\",cts)[,1:2]
Estimate Std. Error
(Intercept) 4.8200993881 0.142381642
b -0.0421189130 0.092620363
(Intercept) 4.7459340076 0.206372906
c 0.0005770324 0.003547885
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。