标签:single tom 排序 pre operator ring str ota lan
使用方式
合并两个数据集,要求两个数据集的列数相等:
rbind(parameter1,parameter2)
合并多个数据集,各个数据集的列数相等:
rbind(parameter1,parameter2,...,parametern)
从数据集中提取数据
test <- rbind()
for (i in 1:length(s_5))
{
test <- rbind(test,data[s_5[i],])
}
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, ...)
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
按照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
横向合并
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
当然merge也可以使用纵向合并
merge(data1,dadta2,all=T)
纵向合并
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
标签:single tom 排序 pre operator ring str ota lan
原文地址:http://www.cnblogs.com/to-creat/p/6230026.html