R 如何使用DF/list索引将数据帧列表连接到一个数据帧?

R 如何使用DF/list索引将数据帧列表连接到一个数据帧?,r,dataframe,join,dplyr,R,Dataframe,Join,Dplyr,这是我的样本数据。小数据帧列表(listofds)我想加入到单个数据帧(点) listofds共有5个小数据帧,共7行,points是一个数据帧,共7行: library(dplyr); library(tibble) 积分事件\u ID\u CNTY year COUNTRY #> #>2013年1月243日。利比亚#对应于DFS清单中的两个项目[[1]] #>2 LBY3389 2015年。利比亚#对应于DFS清单中的一个项目[[2]] #>3 LBY3393

这是我的样本数据。小数据帧列表(
listofds
)我想加入到单个数据帧(

listofds
共有5个小数据帧,共7行,
points
是一个数据帧,共7行:

library(dplyr); library(tibble)
积分事件\u ID\u CNTY year COUNTRY
#>               
#>2013年1月243日。利比亚#对应于DFS清单中的两个项目[[1]]
#>2 LBY3389 2015年。利比亚#对应于DFS清单中的一个项目[[2]]
#>3 LBY3393 2015年。利比亚
#>4 LBY3506 2015年。利比亚
#>5 LBY3822 2015年。利比亚
清单F
#> $`1`
#>#A tibble:2 x 4
#>CELL_ID gwno POP prio_country
#>               
#> 1 165267.   620  751. 利比亚
#> 2 164547.   620  755. 利比亚
#> 
#> $`2`
#>#A tibble:1 x 4
#>CELL_ID gwno POP prio_country
#>                
#> 1 172475.   620 11676. 利比亚
#> 
#> $`3`
#>#A tibble:1 x 4
#>CELL_ID gwno POP prio_country
#>                
#> 1 172475.   620 11676. 利比亚
#> 
#> $`4`
#>#A tibble:1 x 4
#>CELL_ID gwno POP prio_country
#>                
#> 1 172475.   620 11676. 利比亚
#> 
#> $`5`
#>#A tibble:2 x 4
#>CELL_ID gwno POP prio_country
#>               
#> 1 165267.   620  751. 利比亚
#> 2 164547.   620  755. 利比亚
我如何连接这两个,使它们遵循下面的模式?我必须找到一种方法将它们绑定到数据帧的行索引上,还是有一种更优雅的方法

points
#> # A tibble: 5 x 3
#>   EVENT_ID_CNTY  year COUNTRY
#>   <chr>         <dbl> <chr>  
#> 1 LBY1243       2013. Libya  # Corresponds to the two items in listOfDFs[[1]]
#> 2 LBY3389       2015. Libya  # Corresponds to the one item in listOfDFs[[2]]
#> 3 LBY3393       2015. Libya  
#> 4 LBY3506       2015. Libya  
#> 5 LBY3822       2015. Libya


listOfDFs
#> $`1`
#> # A tibble: 2 x 4
#>   CELL_ID  gwno   POP prio_country
#>     <dbl> <int> <dbl> <chr>       
#> 1 165267.   620  751. Libya       
#> 2 164547.   620  755. Libya       
#> 
#> $`2`
#> # A tibble: 1 x 4
#>   CELL_ID  gwno    POP prio_country
#>     <dbl> <int>  <dbl> <chr>       
#> 1 172475.   620 11676. Libya       
#> 
#> $`3`
#> # A tibble: 1 x 4
#>   CELL_ID  gwno    POP prio_country
#>     <dbl> <int>  <dbl> <chr>       
#> 1 172475.   620 11676. Libya       
#> 
#> $`4`
#> # A tibble: 1 x 4
#>   CELL_ID  gwno    POP prio_country
#>     <dbl> <int>  <dbl> <chr>       
#> 1 172475.   620 11676. Libya       
#> 
#> $`5`
#> # A tibble: 2 x 4
#>   CELL_ID  gwno   POP prio_country
#>     <dbl> <int> <dbl> <chr>       
#> 1 165267.   620  751. Libya       
#> 2 164547.   620  755. Libya
#>事件_ID_CNTY year COUNTRY CELL _idgwno POP prio_COUNTRY
#>                       
#>2013年1月243日。利比亚165267。620  751. 利比亚
#>2013年1月243日。利比亚164547。620  755. 利比亚
#>LBY3389 2015。利比亚172475。620 11676. 利比亚
#>2015年LBY3393。利比亚172475。620 11676. 利比亚
#>LBY3506 2015。利比亚172475。620 11676. 利比亚
#>LBY3822 2015。利比亚165267。620  751. 利比亚
#>LBY3822 2015。利比亚164547。620  755. 利比亚
.........

基于该示例,我们使用
列表中每个元素的nrow复制“points”数据集的行,并将列绑定在一起

#>   EVENT_ID_CNTY  year COUNTRY  CELL_ID  gwno   POP prio_country
#>   <chr>         <dbl> <chr>     <dbl> <int> <dbl> <chr>  
#>   LBY1243       2013. Libya     165267.   620  751. Libya 
#>   LBY1243       2013. Libya     164547.   620  755. Libya 
#>   LBY3389       2015. Libya     172475.   620 11676. Libya
#>   LBY3393       2015. Libya     172475.   620 11676. Libya
#>   LBY3506       2015. Libya     172475.   620 11676. Libya
#>   LBY3822       2015. Libya     165267.   620  751. Libya 
#>   LBY3822       2015. Libya     164547.   620  755. Libya
.........

或者使用“EVENT\u ID\u CNTY”设置
列表的名称,以创建ID列并与“points”连接

map_dbl(listOfDFs, nrow) %>% 
      rep(seq_len(nrow(points)), .) %>%
      magrittr::extract(points, ., ) %>%
      bind_cols(bind_rows(listOfDFs))

基于该示例,我们使用
列表
中每个元素的nrow复制“points”数据集的行,并将列绑定在一起

#>   EVENT_ID_CNTY  year COUNTRY  CELL_ID  gwno   POP prio_country
#>   <chr>         <dbl> <chr>     <dbl> <int> <dbl> <chr>  
#>   LBY1243       2013. Libya     165267.   620  751. Libya 
#>   LBY1243       2013. Libya     164547.   620  755. Libya 
#>   LBY3389       2015. Libya     172475.   620 11676. Libya
#>   LBY3393       2015. Libya     172475.   620 11676. Libya
#>   LBY3506       2015. Libya     172475.   620 11676. Libya
#>   LBY3822       2015. Libya     165267.   620  751. Libya 
#>   LBY3822       2015. Libya     164547.   620  755. Libya
.........

或者使用“EVENT\u ID\u CNTY”设置
列表的名称,以创建ID列并与“points”连接

map_dbl(listOfDFs, nrow) %>% 
      rep(seq_len(nrow(points)), .) %>%
      magrittr::extract(points, ., ) %>%
      bind_cols(bind_rows(listOfDFs))

使用
dplyr::bind_rows
dplyr::internal_join
的选项可以是:

listOfDFs %>% 
    set_names(points$EVENT_ID_CNTY) %>% 
    bind_rows(.id = 'EVENT_ID_CNTY') %>% 
    left_join(points)
库(dplyr)
变异(点,rn=as.character(row_number())%>%
内部连接(绑定行(listofds,.id=“Name”),by=c(“rn”=“Name”))%>%
选择(-rn)
##tibble:7 x 7
#事件\u ID \u CNTY year COUNTRY CELL \u ID gwno POP prio\u COUNTRY
#                          
#1 LBY1243 2013利比亚165267 620 751利比亚
#2 LBY1243 2013利比亚164547 620 755利比亚
#3 LBY3389 2015利比亚172475 620 11676利比亚
#4 LBY3393 2015利比亚172475 620 11676利比亚
#5 LBY3506 2015利比亚172475 620 11676利比亚
#6 LBY3822 2015利比亚165267 620 751利比亚
#7 LBY3822 2015利比亚164547 620 755利比亚

使用
dplyr::bind_rows
dplyr::internal_join
的选项可以是:

listOfDFs %>% 
    set_names(points$EVENT_ID_CNTY) %>% 
    bind_rows(.id = 'EVENT_ID_CNTY') %>% 
    left_join(points)
库(dplyr)
变异(点,rn=as.character(row_number())%>%
内部连接(绑定行(listofds,.id=“Name”),by=c(“rn”=“Name”))%>%
选择(-rn)
##tibble:7 x 7
#事件\u ID \u CNTY year COUNTRY CELL \u ID gwno POP prio\u COUNTRY
#                          
#1 LBY1243 2013利比亚165267 620 751利比亚
#2 LBY1243 2013利比亚164547 620 755利比亚
#3 LBY3389 2015利比亚172475 620 11676利比亚
#4 LBY3393 2015利比亚172475 620 11676利比亚
#5 LBY3506 2015利比亚172475 620 11676利比亚
#6 LBY3822 2015利比亚165267 620 751利比亚
#7 LBY3822 2015利比亚164547 620 755利比亚
库(tidyr)
点%>%突变(mm=ListOfds)%%>%n最新
#一个tibble:7x7
事件\u ID \u CNTY year COUNTRY CELL \u ID gwno POP prio\u COUNTRY
1 LBY1243 2013利比亚165267 620 751。利比亚
2 LBY1243 2013利比亚164547 620 755。利比亚
3 LBY3389 2015利比亚172475 620 11676利比亚
4 LBY3393 2015利比亚172475 620 11676利比亚
5 LBY3506 2015利比亚172475 620 11676利比亚
6 LBY3822 2015利比亚165267 620 751。利比亚
7 LBY3822 2015利比亚164547 620 755。利比亚
库(tidyr)
点%>%突变(mm=ListOfds)%%>%n最新
#一个tibble:7x7
事件\u ID \u CNTY year COUNTRY CELL \u ID gwno POP prio\u COUNTRY
1 LBY1243 2013利比亚165267 620 751。利比亚
2 LBY1243 2013利比亚164547 620 755。利比亚
3 LBY3389 2015利比亚172475 620 11676利比亚
4 LBY3393 2015利比亚172475 620 11676利比亚
5 LBY3506 2015利比亚172475 620 11676利比亚
6 LBY3822 2015利比亚165267 620 751。利比亚
7 LBY3822 2015利比亚164547 620 755。利比亚
library(tidyr)
points %>% mutate(mm=listOfDFs) %>% unnest

# A tibble: 7 x 7
  EVENT_ID_CNTY  year COUNTRY CELL_ID  gwno    POP prio_country
  <chr>         <dbl> <chr>     <dbl> <int>  <dbl> <chr>       
1 LBY1243        2013 Libya    165267   620   751. Libya       
2 LBY1243        2013 Libya    164547   620   755. Libya       
3 LBY3389        2015 Libya    172475   620 11676  Libya       
4 LBY3393        2015 Libya    172475   620 11676  Libya       
5 LBY3506        2015 Libya    172475   620 11676  Libya       
6 LBY3822        2015 Libya    165267   620   751. Libya       
7 LBY3822        2015 Libya    164547   620   755. Libya