【发布时间】:2017-04-24 07:06:31
【问题描述】:
我有许多列表,在每个列表中,我需要提取的变量都以稍微不同的方式嵌套。有没有一种简单的方法来搜索变量并提取它?
示例列表
list1 <- list(AccountingSupplierParty = list(Party = list(PartyName = "Company Incorporated", PartyType = "The worst party")), DataSet = "Data Set 1")
list2 <- list(SupplierParty = list(Party = list(PartyName = "Company A/S", PartyType = "The best party")), DataSet = "Data Set 2")
我想提取“PartyName”。如下图所示,在庞大的数据集中学习所有变量组合并不是那么有效:
Company1 <- list1$AccountingSupplierParty$Party$PartyName
Company2 <- list2$SupplierParty$Party$PartyName
我想要的输出是:
"Company Incorporated"
"Company A/S"
【问题讨论】:
-
您应该通过添加
dput(list1)和dput(list2)的结果来创建您的示例reproducible。 -
谢谢,我现在就做例子
-
你可以抽象出变化的部分:
sapply(list(list1, list2), function(x){x[[1]]$Party$PartyName})
标签: r list search extract nested-lists