代码
library(tidyverse)
<- starwars %>%
tb select(1:5) %>%
slice(1:6)
<- tb %>%
tb1 slice(1:3)
<- tb %>%
tb2 slice(4:6)
bind_rows()
根据位置垂直拼接bind_cols()
根据位置水平拼接left_join()
根据ID变量匹配,同时只保留前一个数据的个案right_join()
根据ID变量匹配,同时只保留后一个数据的个案full_join()
根据ID变量匹配,同时保留前后数据的所有个案inner_join()
根据ID变量匹配,同时保留前后数据的共同个案semi_join()
根据ID变量匹配,筛选前一个数据中,和后一个数据的共同个案anti_join()
根据ID变量匹配,删除前一个数据中,和后一个数据的共同个案bind_rows()
library(tidyverse)
<- starwars %>%
tb select(1:5) %>%
slice(1:6)
<- tb %>%
tb1 slice(1:3)
<- tb %>%
tb2 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
bind_cols()
<- tb %>%
tb1 select(1:3)
<- tb %>%
tb2 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
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.
<- tb %>%
tb2 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