11  dplyr 合并

11.1 bind_rows()

代码
library(tidyverse)

tb <- starwars %>%
  select(1:5) %>%
  slice(1:6)

tb1 <- tb %>%
  slice(1:3)

tb2 <- tb %>%
  slice(4:6)
tb1
# A tibble: 3 × 5
  name           height  mass hair_color skin_color 
  <chr>           <int> <dbl> <chr>      <chr>      
1 Luke Skywalker    172    77 blond      fair       
2 C-3PO             167    75 <NA>       gold       
3 R2-D2              96    32 <NA>       white, blue
tb2
# A tibble: 3 × 5
  name        height  mass hair_color  skin_color
  <chr>        <int> <dbl> <chr>       <chr>     
1 Darth Vader    202   136 none        white     
2 Leia Organa    150    49 brown       light     
3 Owen Lars      178   120 brown, grey light     
bind_rows(tb1, tb2)
# A tibble: 6 × 5
  name           height  mass hair_color  skin_color 
  <chr>           <int> <dbl> <chr>       <chr>      
1 Luke Skywalker    172    77 blond       fair       
2 C-3PO             167    75 <NA>        gold       
3 R2-D2              96    32 <NA>        white, blue
4 Darth Vader       202   136 none        white      
5 Leia Organa       150    49 brown       light      
6 Owen Lars         178   120 brown, grey light      

11.2 bind_cols()

代码
tb1 <- tb %>%
  select(1:3)
tb2 <- tb %>%
  select(4:5)
tb1
# A tibble: 6 × 3
  name           height  mass
  <chr>           <int> <dbl>
1 Luke Skywalker    172    77
2 C-3PO             167    75
3 R2-D2              96    32
4 Darth Vader       202   136
5 Leia Organa       150    49
6 Owen Lars         178   120
tb2
# A tibble: 6 × 2
  hair_color  skin_color 
  <chr>       <chr>      
1 blond       fair       
2 <NA>        gold       
3 <NA>        white, blue
4 none        white      
5 brown       light      
6 brown, grey light      
bind_cols(tb1, tb2)
# A tibble: 6 × 5
  name           height  mass hair_color  skin_color 
  <chr>           <int> <dbl> <chr>       <chr>      
1 Luke Skywalker    172    77 blond       fair       
2 C-3PO             167    75 <NA>        gold       
3 R2-D2              96    32 <NA>        white, blue
4 Darth Vader       202   136 none        white      
5 Leia Organa       150    49 brown       light      
6 Owen Lars         178   120 brown, grey light      

11.3 left_join()

tb1 %>%
  left_join(tb2)
Error in `left_join()`:
! `by` must be supplied when `x` and `y` have no common variables.
ℹ Use `cross_join()` to perform a cross-join.
tb2 <- tb %>%
  select(c(1, 4:5))
tb1
# A tibble: 6 × 3
  name           height  mass
  <chr>           <int> <dbl>
1 Luke Skywalker    172    77
2 C-3PO             167    75
3 R2-D2              96    32
4 Darth Vader       202   136
5 Leia Organa       150    49
6 Owen Lars         178   120
tb2
# A tibble: 6 × 3
  name           hair_color  skin_color 
  <chr>          <chr>       <chr>      
1 Luke Skywalker blond       fair       
2 C-3PO          <NA>        gold       
3 R2-D2          <NA>        white, blue
4 Darth Vader    none        white      
5 Leia Organa    brown       light      
6 Owen Lars      brown, grey light      
tb1 %>%
  left_join(tb2)
Joining with `by = join_by(name)`
# A tibble: 6 × 5
  name           height  mass hair_color  skin_color 
  <chr>           <int> <dbl> <chr>       <chr>      
1 Luke Skywalker    172    77 blond       fair       
2 C-3PO             167    75 <NA>        gold       
3 R2-D2              96    32 <NA>        white, blue
4 Darth Vader       202   136 none        white      
5 Leia Organa       150    49 brown       light      
6 Owen Lars         178   120 brown, grey light      
tb1 %>%
  left_join(tb2, by = "name")
# A tibble: 6 × 5
  name           height  mass hair_color  skin_color 
  <chr>           <int> <dbl> <chr>       <chr>      
1 Luke Skywalker    172    77 blond       fair       
2 C-3PO             167    75 <NA>        gold       
3 R2-D2              96    32 <NA>        white, blue
4 Darth Vader       202   136 none        white      
5 Leia Organa       150    49 brown       light      
6 Owen Lars         178   120 brown, grey light