使用操作合并R中的数据



逐个连接新表
  1. 左连接操作

  2. 右连接操作

  3. 内连接操作

  4. 全连接操作通过物种列和save,最后比较结果表。

    old_table <- dplyr:: starwarsNew_table<物种>

您可以使用以下代码:

library(dplyr)
# left join
left_join(old_table, new_table, by = "species")
# right join
right_join(old_table, new_table, by = "species")
# inner join
inner_join(old_table, new_table)
# full join
full_join(old_table, new_table)

输出left_join,例如:

# A tibble: 87 × 15
name               height  mass hair_color    skin_color  eye_color birth_year sex    gender    homeworld species films  vehicles  starships moje_obliba
<chr>               <int> <dbl> <chr>         <chr>       <chr>          <dbl> <chr>  <chr>     <chr>     <chr>   <list> <list>    <list>          <dbl>
1 Luke Skywalker        172    77 blond         fair        blue            19   male   masculine Tatooine  Human   <chr>  <chr [2]> <chr [2]>          10
2 C-3PO                 167    75 NA            gold        yellow         112   none   masculine Tatooine  Droid   <chr>  <chr [0]> <chr [0]>           3
3 R2-D2                  96    32 NA            white, blue red             33   none   masculine Naboo     Droid   <chr>  <chr [0]> <chr [0]>           3
4 Darth Vader           202   136 none          white       yellow          41.9 male   masculine Tatooine  Human   <chr>  <chr [0]> <chr [1]>          10
5 Leia Organa           150    49 brown         light       brown           19   female feminine  Alderaan  Human   <chr>  <chr [1]> <chr [0]>          10
6 Owen Lars             178   120 brown, grey   light       blue            52   male   masculine Tatooine  Human   <chr>  <chr [0]> <chr [0]>          10
7 Beru Whitesun lars    165    75 brown         light       blue            47   female feminine  Tatooine  Human   <chr>  <chr [0]> <chr [0]>          10
8 R5-D4                  97    32 NA            white, red  red             NA   none   masculine Tatooine  Droid   <chr>  <chr [0]> <chr [0]>           3
9 Biggs Darklighter     183    84 black         light       brown           24   male   masculine Tatooine  Human   <chr>  <chr [0]> <chr [1]>          10
10 Obi-Wan Kenobi        182    77 auburn, white fair        blue-gray       57   male   masculine Stewjon   Human   <chr>  <chr [1]> <chr [5]>          10
# … with 77 more rows

数据
old_table <- dplyr::starwars 
new_table <- data.frame(species=c("Human", "Ewok", "Droid", "Wookiee", "Gungan"), 
moje_obliba = c(10,5,3,10,9))

最新更新