如何重复加入 r 中的表?

Posted

技术标签:

【中文标题】如何重复加入 r 中的表?【英文标题】:How do I join repeatedly the tables in r? 【发布时间】:2019-11-25 19:19:31 【问题描述】:

我想在两个表之间重复加入。这是表格。

结构(列表(键=结构(1:4,.Label = c(“A”,“B”,“C”,“D”), class= "因子")), class= "data.frame", row.names = c(NA, -4L))

结构(列表(键=结构(c(1L,2L,2L,3L),.Label = c(“A”, "B", "C"), class= "因子"), 源 = 结构(c(1L, 1L, 2L, 2L), .Label = c("a", "b"), class= "因子" ), value = c(1L, 1L, 2L, 2L)), class= "data.frame", row.names = c(NA, -4L))

<joined>
 key
   A
   B
   C
   D

<joining> 
key source value
 A      a     1
 B      a     1
 B      b     2
 C      b     2

如果我使用left_join函数,比如left_join(joined, join, by = "key"),结果就在这里。

  key source value
1   A      a     1
2   B      a     1
3   B      b     2
4   C      b     2
5   D   <NA>    NA

但是,我想按“来源”加入分组。我的预期结果就在这里。

joining_a <- joining %>%
  filter(source == "a")

joining_b <- joining %>%
  filter(source == "b")

left_join(joined, joining_a, by = "key")
left_join(joined, joining_b, by = "key")

bind_rows(left_join(joined, joining_a, by = "key"), left_join(joined, joining_b, by = "key"))

  key source value
1   A      a     1
2   B      a     1
3   C   <NA>    NA
4   D   <NA>    NA
5   A   <NA>    NA
6   B      b     2
7   C      b     2
8   D   <NA>    NA

如何加入不分割这些表的表?

【问题讨论】:

我想你可以把它放在一个列表中并使用maplist(joining, joining) %&gt;% map_dfr(~ left_join(joined, .x, by = "key")) 【参考方案1】:

我们可以将group_split(或split from base R)“加入”到list,然后使用mapleft_join与“加入”一起进行

library(tidyverse)
joining %>% 
    group_split(source) %>%
    map_dfr(~ left_join(joined, .x, by = 'key'))
#  key source value
#1   A      a     1
#2   B      a     1
#3   C   <NA>    NA
#4   D   <NA>    NA
#5   A   <NA>    NA
#6   B      b     2
#7   C      b     2
#8   D   <NA>    NA

或者没有 lambda 函数

joining %>%
    group_split(source) %>%
    map_dfr(left_join, x = joined, by = 'key')

数据

joined <- structure(list(key = structure(1:4, .Label = c("A", "B", "C", 
"D"), class = "factor")), class = "data.frame", row.names = c(NA, 
-4L))

joining <- structure(list(key = structure(c(1L, 2L, 2L, 3L), 
.Label = c("A", 
"B", "C"), class = "factor"), source = structure(c(1L, 1L, 2L, 
2L), .Label = c("a", "b"), class = "factor"), value = c(1L, 1L, 
2L, 2L)), class = "data.frame", row.names = c(NA, -4L))

【讨论】:

以上是关于如何重复加入 r 中的表?的主要内容,如果未能解决你的问题,请参考以下文章

如何在没有任何重复行的情况下连接两个表中的表?

如何加入名称作为值存储在另一个表中的表?

如何从 SQL Server 中的表中删除重复行 [重复]

如何从mysql中的表中删除重复的行

如何找出数据库中的表大小[重复]

如何对具有多列的表中的数据进行分组[重复]