merge是R语言中用来合并数据框的函数
merge函数的声明:
1
2
3
4
|
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, ...)
|
merge函数参数的说明:
x,y:用于合并的两个数据框
by,by.x,by.y:指定依据哪些行合并数据框,默认值为相同列名的列.
all,all.x,all.y:指定x和y的行是否应该全在输出文件.
sort:by指定的列是否要排序.
suffixes:指定除by外相同列名的后缀.
incomparables:指定by中哪些单元不进行合并.
例子:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
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
|
按照NAME, SCHOOL, CLASS合并w1和w2:
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
|
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
1 A S1 4 90 NA NA
2 A S1 10 85 NA NA
3 A S1 11 90 NA NA
4 A S3 5 NA 80 88
5 B S2 5 50 89 81
6 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
1 A S1 10 85 <NA> NA NA NA
2 A S1 4 90 <NA> NA NA NA
3 A S1 11 90 <NA> NA NA NA
4 A <NA> NA NA S3 5 80 88
5 B S2 5 50 S2 5 89 81
6 C S1 1 12 S1 1 55 32
|
将数据框中的NA变成0
dataframe[is.na(dataframe)]
|
请发表评论