R拆分列表的分层列表

R拆分列表的分层列表,r,list,R,List,我有一份清单。我想把这个列表分成两个列表。 最好用例子来解释。下面是一个例子 列表由3个列表组成。 每个列表元素都是一个包含2个data.frames的列表。(人、车) str(listMain)的输出: 我想把这个列表分成两个列表。 第一个应该是个人主义者,第二个是卡莱斯特。像这样 > listPerson <- list(df1,df2,df3) > str(listPerson) List of 3 $ :'data.frame': 1 obs. of 2 var

我有一份清单。我想把这个列表分成两个列表。 最好用例子来解释。下面是一个例子

列表由3个列表组成。 每个列表元素都是一个包含2个data.frames的列表。(人、车)

str(listMain)
的输出:

我想把这个列表分成两个列表。 第一个应该是个人主义者,第二个是卡莱斯特。像这样

> 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
listPerson str(listPerson) 3人名单 $:'data.frame':1个obs。共有2个变量: ..$id:num 1 ..$name:系数w/1级“约翰”:1 $:'data.frame':1个obs。共有2个变量: ..$id:num 1 ..$name:系数w/1级“约翰”:1 $:'data.frame':1个obs。共有2个变量: ..$id:num 1 ..$name:系数w/1级“约翰”:1 >listCars街(listCars) 3人名单 $:'data.frame':1个obs。共有2个变量: ..$id:num 1 ..$car:系数w/1级“欧宝”:1 $:'data.frame':1个obs。共有2个变量: ..$id:num 1 ..$car:系数w/1级“欧宝”:1 $:'data.frame':1个obs。共有2个变量: ..$id:num 1 ..$car:系数w/1级“欧宝”:1 怎么做?
提前感谢。

您可以使用
lappy
遍历
listMain
中的每个元素并提取第一(第二)个元素。我认为这里的关键点是像
lappy
中的任何“传统”函数一样使用
[[

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

要阅读此函数的文档,请键入
?`[[`

您可以使用
lappy
遍历
listMain
中的每个元素并提取第一(第二)个元素。我认为这里的关键点是使用
[
lappy
中的任何“传统”函数一样

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

要阅读此函数的文档,请键入
?`[[`

您可以使用
purrr::transpose
将列表从包含2个元素的3个元素翻转到包含3个元素的2个元素:

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
如果您确实需要两个单独的列表,
purrr::map
将自动为您传递一个字符串或数字:

map(listMain, 'person')

## [[1]]
##   id name
## 1  1 John
## 
## [[2]]
##   id name
## 1  1 John
## 
## [[3]]
##   id name
## 1  1 John
…或使用带有
*\u df
后缀的版本在子集设置后简化为data.frame:

map_df(listMain, 'person')

##   id name
## 1  1 John
## 2  1 John
## 3  1 John

您可以使用
purrr::transpose
将列表从包含2个元素的3个元素翻转到包含3个元素的2个元素:

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
如果您确实需要两个单独的列表,
purrr::map
将自动为您传递一个字符串或数字:

map(listMain, 'person')

## [[1]]
##   id name
## 1  1 John
## 
## [[2]]
##   id name
## 1  1 John
## 
## [[3]]
##   id name
## 1  1 John
…或使用带有
*\u df
后缀的版本在子集设置后简化为data.frame:

map_df(listMain, 'person')

##   id name
## 1  1 John
## 2  1 John
## 3  1 John