在多维列表中选择通配符? [英] Select wildcard in multidimensional list?

查看:63
本文介绍了在多维列表中选择通配符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在R中,我有一个多维列表.我可以访问以下元素:

In R, I have a multidimensional list. I can access elements like:

results[["A1"]][["B2"]][["C1"]]

当然,以上语句的输出也是一个列表.

Of course, the output of the above statement is also a list.

我想做的事情是这样的:

What I'd like to do is something like:

results[["A1"]][[*]][["C1"]]

results[["A1"]][[,]][["C1"]]

我想要的输出将是一个大列表,该列表是第二维中每个值(例如,"B1","B2",...)的列表的串联.

My desired output would be one big list, which is the concatenation of the lists for each value (e.g., "B1", "B2", ...) in the second dimension.

像这样可能吗?我知道我可以迭代所需的尺寸:

Is something like this possible? I know that I can iterate over the desired dimension:

for (i in 1:length(results[["A1"]])){
    output = c(output, results[["A1"]][[i]][["C1"]]
}

但是我想知道是否还有清洁剂?我做错了吗?

but I was wondering if there's anything cleaner? Am I doing it all wrong?

推荐答案

这项工作有效吗?

sapply( sapply(results[["A1]], "[[", TRUE), "[[", "C1"")

将特定索引名称"D"推广到第四级的方法是:

The generalization to a fourth level for a specific index name "D" would just be:

sapply( sapply( sapply(results[["A1"]], "[[", TRUE), "[[", "C1"), "[[", "D")

从内到外读取,因为需要处理嵌套函数,所以会收集所有results$A1值的子列表,然后进行处理以构造仅包含"C1"名称的列表,然后将其简化进一步从中提取列表以产生形式为results$A1$all-wildcards$C1$D的值.鉴于"[["函数将评估其参数这一事实,如果在适当的索引级别上它们是有效的列表名称,则可以提供一个具有多个值的未引用对象.

Reading from the inside out, as one needs to do with nested function calls, all of sublists of the results$A1 values are collected and then processed to construct a list of only those with "C1" names, and then that reduced list is further extracted from to yield values of the form results$A1$all-wildcards$C1$D. Given the fact that the "[[" function will evaluate its argument you could have offer an unquoted object that had multiple values if they were valid list names at the appropriate level of indexing.

这篇关于在多维列表中选择通配符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆