首页 > 代码库 > R语言-merge和rbind

R语言-merge和rbind

rbind

使用方式 
合并两个数据集,要求两个数据集的列数相等:

rbind(parameter1,parameter2)
  • 1
  • 1

合并多个数据集,各个数据集的列数相等:

rbind(parameter1,parameter2,...,parametern)
  • 1
  • 1

从数据集中提取数据

test <- rbind()
for (i in 1:length(s_5))
{
     test <- rbind(test,data[s_5[i],])
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 1
  • 2
  • 3
  • 4
  • 5

merge

merge函数的声明:

merge(x, y, by = intersect(names(x), names(y)),
      by.x = by, by.y = by, all = FALSE, all.x = all, all.y = all,
      sort = TRUE, suffixes = c(".x",".y"),
      incomparables = NULL, ...)
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

merge函数参数的说明:

参数说明
x,y 用于合并的两个数据框
by,by.x,by.y 指定依据哪些行合并数据框,默认值为相同列名的列.
all,all.x,all.y 指定x和y的行是否应该全在输出文件.
sort by指定的列是否要排序.
suffixes 指定除by外相同列名的后缀.
incomparables 指定by中哪些单元不进行合并.

例子:

w1:
NAME    SCHOOL    CLASS    ENGLISH
A    S1    10    85
B    S2    5    50
A    S1    4    90
A    S1    11    90
C    S1    1    12

w2:
NAME    SCHOOL    CLASS    MATHS    ENGLISH
A    S3    5    80    88
B    S2    5    89    81
C    S1    1    55    32
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

按照NAME, SCHOOL, CLASS合并w1和w2:

merge(w1, w2, all = T)

  NAME SCHOOL CLASS ENGLISH MATHS
1    A     S1     4      90    NA
2    A     S1    10      85    NA
3    A     S1    11      90    NA
4    A     S3     5      88    80
5    B     S2     5      50    NA
6    B     S2     5      81    89
7    C     S1     1      12    NA
8    C     S1     1      32    55

merge(w1, w2, by = c("NAME", "SCHOOL", "CLASS"), all = T)

  NAME SCHOOL CLASS ENGLISH.x MATHS ENGLISH.y
    A     S1     4        90    NA        NA
    A     S1    10        85    NA        NA
    A     S1    11        90    NA        NA
    A     S3     5        NA    80        88
    B     S2     5        50    89        81
    C     S1     1        12    55        32

merge(w1, w2, all = T, incomparables = "A")
Error in merge.data.frame(w1, w2, all = T, incomparables = "A") :
  ‘incomparables‘ is supported only for merging on a single column

merge(w1, w2, all = T, by = "NAME", incomparables = "A")
  NAME SCHOOL.x CLASS.x ENGLISH.x SCHOOL.y CLASS.y MATHS ENGLISH.y
    A       S1      10        85     <NA>      NA    NA        NA
    A       S1       4        90     <NA>      NA    NA        NA
    A       S1      11        90     <NA>      NA    NA        NA
    A     <NA>      NA        NA       S3       5    80        88
    B       S2       5        50       S2       5    89        81
    C       S1       1        12       S1       1    55        32
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34

横向合并

ID<-c(1,2,3,4)
name<-c("Jim","Tony","Lisa","Tom")
score<-c(89,22,78,78)
student1<-data.frame(ID,name)
student2<-data.frame(ID,score)
total_student<-merge(student1,student2,by="ID")
total_student
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

当然merge也可以使用纵向合并

merge(data1,dadta2,all=T)
  • 1
  • 1

纵向合并

ID<-c(1,2,3)
name<-c("Jame","Kevin","Sunny")
student1<-data.frame(ID,name)
ID<-c(4,5,6)
name<-c("Sun","Frame","Eric")
student2<-data.frame(ID,name)
total<-rbind(student1,student2)
total

R语言-merge和rbind