前⾯面已经介绍过 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))
inner_join(x, y) :选出在y中含有相同元素所对应的x,将⼆二者的⾏行行进⾏行行合并,以 x(superheroes)的顺序为准
(ijps <- inner_join(publishers, superheroes))
inner_join(x, y) :选出在y中含有相同元素所对应的x,将⼆二者的⾏行行进⾏行行合并,以 x(superheroes)的顺序为准
2.semi_join
(sjsp <- semi_join(superheroes, publishers))
semi_join(x, y) :取半,从y中选出在x中有的元素所对应的⾏行行,不不加y(publishers)
(sjps <- semi_join(x = publishers, y = superheroes))
semi_join(x, y) :取半,从y中选出在x中有的元素所对应的⾏行行,不不加y(superheroes)
3.left_join
(ljsp <- left_join(superheroes, publishers))
left_join(x, y) :以x为准,若y中不不含有x(Hellboy)中的元素,也以x为主,计作NA
(ljps <- left_join(publishers, superheroes))
left_join(x, y) :以x为准,若y中不不含有x(Image)中的元素,也以x为主,计作NA
4.anti_join
(ajsp <- anti_join(superheroes, publishers))
anti_join(x, y) :选出x不在y(Hellboy)⾥里里的
(ajps <- anti_join(publishers, superheroes))
anti_join(x, y) :选出x不不在y(Image)⾥里里的
5.full_join
(fjsp <- full_join(superheroes, publishers))
full_join(x, y) :x、y全部合并,若⼆二者互相都有不不含有对⽅方的,都计作NA.
最后友情宣传生信技能树
全国巡讲:R基础,Linux基础和RNA-seq实战演练 : 预告:12月28-30长沙站