list 如何从 R 中的列表列表中提取元素?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23758858/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I extract elements from lists of lists in R?
提问by Akos
I have a bunch of lists containing lists within them (generalised linear model output). I want to write a function which will extract several elements from each list and then combine the results into a data frame.
我有一堆列表,其中包含列表(广义线性模型输出)。我想编写一个函数,该函数将从每个列表中提取多个元素,然后将结果组合到一个数据框中。
I want to extract modelset[[1]]$likelihood
& modelset[[1]]$fixef
, modelset[[2]]$likelihood
& modelset[[2]]$fixef
, etc, and combine the results into a data frame.
我想提取modelset[[1]]$likelihood
& modelset[[1]]$fixef
、modelset[[2]]$likelihood
&modelset[[2]]$fixef
等,并将结果组合成一个数据框。
Can someone give me an idea of how to do this?
有人可以给我一个如何做到这一点的想法吗?
Apologies if my question is confusing: what I am trying to do is beyond my limited programming understanding.
如果我的问题令人困惑,我深表歉意:我想要做的超出了我有限的编程理解。
Further information about my list:
关于我的名单的更多信息:
modelset: Large list (16 elements, 7.3Mb)
:List of 29
..$ fixef : Named num [1:2] -1.236 -0.611
.. ..- attr(*, "names")= chr [1:2] "(Intercept)" "SMIstd"
..$ likelihood :List of 4
.. ..$ hlik: num 238
.. ..$ pvh : num 256
.. ..$ pbvh: num 260
.. ..$ cAIC: num 567
...etc
回答by Konrad Rudolph
In order to solve this elegantly you need to understand that you can use ['…']
instead of $…
to access list elements (but you will get a list back instead of an individual element).
为了优雅地解决这个问题,您需要了解您可以使用['…']
而不是$…
访问列表元素(但您将返回一个列表而不是单个元素)。
So if you want to get the elements likelihood
and fixef
, you can write:
所以如果你想获取元素likelihood
and fixef
,你可以写:
modelset[[1]][c('likelihood', 'fixef')]
Now you want to do that for each element in modelset
. That's what lapply
does:
现在您想对modelset
. 这就是lapply
它的作用:
lapply(modelset, function (x) x[c('likelihood', 'fixef')])
This works, but it's not very R-like.
这有效,但它不是很像 R。
You see, in R, almost everythingis a function. […]
is calling a function named [
(but since [
is a special symbol for R, in needs to be quoted in backticks: `[`
). So you can instead write this:
你看,在 R 中,几乎所有东西都是函数。[…]
正在调用一个名为的函数[
(但由于它[
是 R 的特殊符号,因此需要在反引号中引用:)`[`
。所以你可以这样写:
lapply(modelset, function (x) `[`(c('likelihood', 'fixef')])
Wow, that's not very readable at all. However, we can now remove the wrapping anonymous function (x)
, since inside we're just calling another function, and move the extra arguments to the last parameter of lapply
:
哇,这根本不是很可读。但是,我们现在可以删除包装匿名function (x)
,因为在内部我们只是调用另一个函数,并将额外的参数移动到的最后一个参数lapply
:
lapply(modelset, `[`, c('likelihood', 'fixef'))
This works and is elegant R code.
这有效并且是优雅的 R 代码。
Let's step back and re-examine what we did here. In effect, we had an expression which looked like this:
让我们退后一步,重新审视我们在这里所做的事情。实际上,我们有一个如下所示的表达式:
lapply(some_list, function (x) f(x, y))
And this call can instead be written as
而这个调用可以写成
lapply(some_list, f, y)
We did exactly that, with somelist = modelset
, f = `[`
and y = c('likelihood', 'fixef')
.
我们正是这样做的,使用somelist = modelset
,f = `[`
和y = c('likelihood', 'fixef')
。