【问题标题】:split and combine a lot of lists by element in R在R中按元素拆分和组合大量列表
【发布时间】:2021-08-11 06:38:13
【问题描述】:

从上一步读取一些 rds 对象,我有很多(超过 500 个)R 列表。 每个列表都包含不同类型的对象。一些列表包含数据框、一些其他参数、一些其他模型...... 只是为了说明这个概念,让我们假设谁有:

customer_list

customer_list$deutsch
customer_list$nederlands
customer_list$spain
customer_list$france
......

product_list

product_list$deutsch
product_list$nederlands
product_list$spain
product_list$france

我想以这种方式将我的所有列表拆分/组合成一个关于名称的对象:

    deutsch$product
    deutsch$customer


nederlands$customer
nederlands$products
    ....

等等。

列表列表也很有用:

    huge_list$deutsch
    huge_list$deutsch$customer_list
    huge_list$deutsch$product_list
.......
   

【问题讨论】:

  • 请添加一个可重现的例子。

标签: r list


【解决方案1】:

您可以尝试使用Map -

#Example
customer_list <- list(deutsch = head(mtcars), nederlands = head(iris))
product_list <- list(deutsch = head(iris), nederlands = head(mtcars))

result <- Map(function(x, y) list(customer_list = x, product_list = y), 
    customer_list[names(product_list)], product_list)

result 中,您将拥有result$deutsch$product_listresult$deutsch$customer_listresult$nederlands$product_listresult$nederlands$customer_list

【讨论】:

  • 对于超过 500 个列表对象,这似乎有点麻烦。我将尝试将其转换为循环处理所有内容。谢谢
猜你喜欢
  • 2019-09-10
  • 2022-06-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多