2016-08-25 12 views
0

私はリストのリストを持っています。このリストを2つのリストに分割したいと思います。 例で説明する方が良いです。ここに例があります。Rリストの階層リスト

リストは3つのリストで構成されています。 リスト要素のそれぞれは、2つのdata.framesのリストです。 (人、車)

> df1 = data.frame(id = 1, name = "John") 
> df2 = df1 
> df3 = df1 
> df4 = data.frame(id = 1, car = "Opel") 
> df5 = df4 
> df6 = df5 
> list1 = list(person = df1,car=df4) 
> list2 = list(person = df2,car=df5) 
> list3 = list(person = df3,car=df6) 
> listMain = list(list1,list2,list3) 
> str(listMain) 
List of 3 
$ :List of 2 
    ..$ person:'data.frame': 1 obs. of 2 variables: 
    .. ..$ id : num 1 
    .. ..$ name: Factor w/ 1 level "John": 1 
    ..$ car :'data.frame': 1 obs. of 2 variables: 
    .. ..$ id : num 1 
    .. ..$ car: Factor w/ 1 level "Opel": 1 
$ :List of 2 
    ..$ person:'data.frame': 1 obs. of 2 variables: 
    .. ..$ id : num 1 
    .. ..$ name: Factor w/ 1 level "John": 1 
    ..$ car :'data.frame': 1 obs. of 2 variables: 
    .. ..$ id : num 1 
    .. ..$ car: Factor w/ 1 level "Opel": 1 
$ :List of 2 
    ..$ person:'data.frame': 1 obs. of 2 variables: 
    .. ..$ id : num 1 
    .. ..$ name: Factor w/ 1 level "John": 1 
    ..$ car :'data.frame': 1 obs. of 2 variables: 
    .. ..$ id : num 1 
    .. ..$ car: Factor w/ 1 level "Opel": 1 

このリストを2つのリストに分割します。 最初はpersonList、2番目はcarListです。このような。

> listPerson <- list(df1,df2,df3) 
> str(listPerson) 
List of 3 
$ :'data.frame': 1 obs. of 2 variables: 
    ..$ id : num 1 
    ..$ name: Factor w/ 1 level "John": 1 
$ :'data.frame': 1 obs. of 2 variables: 
    ..$ id : num 1 
    ..$ name: Factor w/ 1 level "John": 1 
$ :'data.frame': 1 obs. of 2 variables: 
    ..$ id : num 1 
    ..$ name: Factor w/ 1 level "John": 1 
> listCars <- list(df4,df5,df6) 
> str(listCars) 
List of 3 
$ :'data.frame': 1 obs. of 2 variables: 
    ..$ id : num 1 
    ..$ car: Factor w/ 1 level "Opel": 1 
$ :'data.frame': 1 obs. of 2 variables: 
    ..$ id : num 1 
    ..$ car: Factor w/ 1 level "Opel": 1 
$ :'data.frame': 1 obs. of 2 variables: 
    ..$ id : num 1 
    ..$ car: Factor w/ 1 level "Opel": 1 

どうすればよいですか? ありがとうございます。

答えて

2

lapplyを使用してlistMainの各要素を調べ、最初の(2番目の)要素を抽出できます。ここでのポイントは、lapplyの "伝統的な"機能のような[[の使用です。

listPerson <- lapply(listMain, `[[`, 1) 
listCar <- lapply(listMain, `[[`, 2) 

@Frankはコメントで示唆しているように、名前でサブセットすることもできます。重要な点は、通常の機能のように[[を使用できることです。

listPerson <- lapply(listMain, `[[`, "person") 

この機能のドキュメントを読むには、?`[[`と入力してください。

0

あなたは3それぞれを含む2の2をそれぞれ含む3つの要素からリストを反転させるpurrr::transposeを使用することができます。

library(purrr) 

tListMain <- transpose(listMain) 

str(tListMain) 

## List of 2 
## $ person:List of 3 
## ..$ :'data.frame': 1 obs. of 2 variables: 
## .. ..$ id : num 1 
## .. ..$ name: Factor w/ 1 level "John": 1 
## ..$ :'data.frame': 1 obs. of 2 variables: 
## .. ..$ id : num 1 
## .. ..$ name: Factor w/ 1 level "John": 1 
## ..$ :'data.frame': 1 obs. of 2 variables: 
## .. ..$ id : num 1 
## .. ..$ name: Factor w/ 1 level "John": 1 
## $ car :List of 3 
## ..$ :'data.frame': 1 obs. of 2 variables: 
## .. ..$ id : num 1 
## .. ..$ car: Factor w/ 1 level "Opel": 1 
## ..$ :'data.frame': 1 obs. of 2 variables: 
## .. ..$ id : num 1 
## .. ..$ car: Factor w/ 1 level "Opel": 1 
## ..$ :'data.frame': 1 obs. of 2 variables: 
## .. ..$ id : num 1 
## .. ..$ car: Factor w/ 1 level "Opel": 1 

...またはmapを持つ単一のdata.frame、lapplyのバージョンに、それぞれを組み合わせて似およびdplyr::bind_rowsdo.call(rbind, ...)へ:あなたは実際には2つの別々のリストをしたい場合は、その文字列または数値渡す場合、purrr::mapが自動的にサブセットます

listMain %>% transpose() %>% map(dplyr::bind_rows) # purrr pipes well 

## $person 
## id name 
## 1 1 John 
## 2 1 John 
## 3 1 John 
## 
## $car 
## id car 
## 1 1 Opel 
## 2 1 Opel 
## 3 1 Opel 

map(listMain, 'person') 

## [[1]] 
## id name 
## 1 1 John 
## 
## [[2]] 
## id name 
## 1 1 John 
## 
## [[3]] 
## id name 
## 1 1 John 
...またはサブセット後data.frameに簡素化するために *_df接尾辞のバージョンを使用します。

map_df(listMain, 'person') 

## id name 
## 1 1 John 
## 2 1 John 
## 3 1 John 
関連する問題