将列表矩阵转换为常规矩阵
采用以下代码:
foo <- list()
foo[[1]] <- list(a=1, b=2)
foo[[2]] <- list(a=11, b=22)
foo[[3]] <- list(a=111, b=222)
result <- do.call(rbind, foo)
result[,'a']
在本例中,result[,'a']
显示一个列表。是否有更优雅的方式使 result
成为“常规”向量矩阵?我想有一些手动方法可以解决这个问题,但我想知道我是否遗漏了一个明显的步骤。
Take the following code:
foo <- list()
foo[[1]] <- list(a=1, b=2)
foo[[2]] <- list(a=11, b=22)
foo[[3]] <- list(a=111, b=222)
result <- do.call(rbind, foo)
result[,'a']
In this case, result[,'a']
shows a list. Is there a more elegant way such that result
is a "regular" matrix of vectors? I imagine there are manual ways of going about this, but I was wondering if there was an obvious step that I was missing.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
列表上的 do.call 非常优雅且快速。事实上,当我需要组合一个巨大的列表时, do.call(rbind, my.list) 曾经救了我的命。这是迄今为止最快的解决方案。
为了解决你的问题,也许是这样的:
do.call on lists is very elegant, and fast. In fact do.call(rbind, my.list) once saved my ass when I needed to combine a huge list. It was by far the fastest solution.
To solve your problem, maybe something like:
一种可能的解决方案如下(但我对替代方案感兴趣):
One possible solution is as follows (but am interested in alternatives):