美文网首页R基础
dplyr包中的inner_join、semi_join、lef

dplyr包中的inner_join、semi_join、lef

作者: 小梦游仙境 | 来源:发表于2019-11-25 22:34 被阅读0次

前⾯面已经介绍过 rbind 和 cbind 函数的⽤用法,如下:

rbind : 根据⾏行行进⾏行行合并,就是⾏行行的叠加,m⾏行行的矩阵与n⾏行行的矩阵rbind()最后变成m+n⾏行行,合 并前提:列列数必需相等; cbind : 根据列列进⾏行行合并,即叠加所有列列,m列列的矩阵与n列列的矩阵 cbind()最后变成m+n列列,合并前提:⾏行行数必需相符。

但是,当两个数据框,含有的⾏行行数、列列数不不相等的时候,改如何进⾏行行 合并 呢?

tidyverse 中的 inner_join 、 semi_join 、 left_join 、 anti_join 、 full_join 函数值 得学习。

install.packages('tidyverse')
superheroes <- tibble::tribble(
  ~name, ~alignment,  ~gender,          ~publisher,
  "Magneto",      "bad",   "male",            "Marvel",
  "Storm",     "good", "female",            "Marvel",
  "Mystique",      "bad", "female",            "Marvel",
  "Batman",     "good",   "male",                "DC",
  "Joker",      "bad",   "male",                "DC",
  "Catwoman",      "bad", "female",                "DC",
  "Hellboy",     "good",   "male", "Dark Horse Comics"
)
superheroes

publishers <- tibble::tribble(
  ~publisher, ~yr_founded,
  "DC",       1934L,
  "Marvel",       1939L,
  "Image",       1992L
)

1.inner_join

  (ijsp <- inner_join(superheroes, publishers))
image-20191118173444647

inner_join(x, y) :选出在y中含有相同元素所对应的x,将⼆二者的⾏行行进⾏行行合并,以 x(superheroes)的顺序为准

(ijps <- inner_join(publishers, superheroes))   
image-20191118175539522

inner_join(x, y) :选出在y中含有相同元素所对应的x,将⼆二者的⾏行行进⾏行行合并,以 x(superheroes)的顺序为准

2.semi_join

(sjsp <- semi_join(superheroes, publishers))
image-20191118174031977

semi_join(x, y) :取半,从y中选出在x中有的元素所对应的⾏行行,不不加y(publishers)

(sjps <- semi_join(x = publishers, y = superheroes))
image-20191118175648452

semi_join(x, y) :取半,从y中选出在x中有的元素所对应的⾏行行,不不加y(superheroes)

3.left_join

(ljsp <- left_join(superheroes, publishers))
image-20191118175054555

left_join(x, y) :以x为准,若y中不不含有x(Hellboy)中的元素,也以x为主,计作NA

(ljps <- left_join(publishers, superheroes))
image-20191118175853715

left_join(x, y) :以x为准,若y中不不含有x(Image)中的元素,也以x为主,计作NA

4.anti_join

(ajsp <- anti_join(superheroes, publishers))
image-20191118175245433

anti_join(x, y) :选出x不在y(Hellboy)⾥里里的

 (ajps <- anti_join(publishers, superheroes))
image-20191118175929515

anti_join(x, y) :选出x不不在y(Image)⾥里里的

5.full_join

  (fjsp <- full_join(superheroes, publishers))
image-20191118180010757

full_join(x, y) :x、y全部合并,若⼆二者互相都有不不含有对⽅方的,都计作NA.

最后友情宣传生信技能树

相关文章

网友评论

    本文标题:dplyr包中的inner_join、semi_join、lef

    本文链接:https://www.haomeiwen.com/subject/lrjswctx.html