我想在两个表之间反复连接。这是桌子。
structure(list(key = structure(1:4,.Label = c(“ A”,“ B”,“ C”,“ D”), class =“ factor”)),class =“ data.frame”,row.names = c(NA, -4L))
structure(list(key = structure(c(1L,2L,2L,3L),.Label = c(“ A”, “ B”,“ C”),class =“ factor”),source =结构(c(1L,1L,2L,2L),.Label = c(“ a”,“ b”),class =“ factor” ),值= c(1L,1L,2L,2L)),类=“ 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函数(join,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
我如何不将这些表划分表?
答案 0 :(得分:1)
我们可以group_split
(或split
中的base R
)将“加入”到list
中,然后使用{ {1}}
left_join
或者没有lambda函数
map
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