2017-04-24 9 views
0

목록의 목록이 여러 개 있으며 각 목록에서 추출해야하는 변수는 약간 다른 방식으로 중첩되어 있습니다. 변수를 검색하고 추출하는 간단한 방법이 있습니까?R : 목록 목록에서 변수 검색 및 추출

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" 
+0

당신은 당신의 예를 확인해야합니다 [재현] (http://stackoverflow.com/questions/5963269/how-to-make-a-great -r-reproducible-example # 5963610)에'dput (list1)'과'dput (list2)'의 결과를 더한다. – alistaire

+0

감사합니다, 지금 예제를 만들 것입니다 –

+0

변경된 부분을 추상화 할 수 있습니다 :'sapply (list (list1, list2), function (x) {x [[1]] $ Party $ PartyName})' – alistaire

답변

2

은 각 목록을 목록에서 제외하고 PartyName에서 끝나지 않는 모든 것을 걸러 낼 수 있습니다.

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") 

c1 <- unlist(list1) 
c1 <- c1[grepl("PartyName$", names(c1))] 

AccountingSupplierParty.Party.PartyName 
       "Company Incorporated" 

c2 <- unlist(list2) 
c2 <- c2[grepl("PartyName$", names(c2))] 
c2 

SupplierParty.Party.PartyName 
       "Company A/S" 
+0

Thanks 이 트릭을 않습니다. –

0

당신은 RLIST 라이브러리를 시도 할 수 있습니다 아래 그림과 같이 거대한 데이터 세트에서 변수의 모든 조합을 배울 수 있도록 효율적이지 않습니다.
https://cran.r-project.org/web/packages/rlist/rlist.pdf

library(rlist) 
list.flatten(list1)[1] 
list.flatten(list2)[1] 

$AccountingSupplierParty.Party.PartyName 
[1] "Company Incorporated" 
$SupplierParty.Party.PartyName 
[1] "Company A/S" 
0

당신은 stringAsFactors 작업을 할 수 있기를 두 번 내가 data.frame 사용이

dfs <- data.frame(lapply(list1, data.frame, stringsAsFactors = FALSE)) 
df1[ , grepl("PartyName" , names(df1)) ] 

공지 사항을 시도 할 수 있습니다. 이 당신에게 줄 것이다

하는 데 도움이
[1] "Company Incorporated" 

희망, 움베르토