Rbind
How to use
Merging two datasets requires equal number of columns for both datasets:
rbind(parameter1,parameter2)
Merging multiple datasets, the number of columns in each data set is equal:
rbind(parameter1,parameter2,...,parametern)
Extracting data from a data set
test <- rbind()forin1:length(s_5)){ test <- rbind(test,data[s_5[i],])}
Merge
Declaration of the Merge function:
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, ...)
Description of the merge function parameter:
Parameters |
Description |
X, y |
Two data frames for merging |
By,by.x,by.y |
Specifies the rows by which the data frame is merged, with the default value being the column with the same column name. |
All,all.x,all.y |
Specifies whether the rows of x and y should be all in the output file. |
Sort |
Whether the column specified by is to be sorted. |
Suffixes |
Specifies the suffix of the same column name, except by. |
Incomparables |
Specifies which cells in by are not merged. |
Example:
W1:NAME SCHOOL CLASS 中文版AS1Ten -B S25 -AS14 -AS1 One -C S11 AW2:NAME SCHOOL CLASS MATHS 中文版AS35 the theB S25 the BayiC S11 - +
By name, SCHOOL, class merges W1 and W2:
Merge (W1, w2, all =T) NAME SCHOOL CLASS 中文版 MATHS1A S14 - NA2A S1Ten - NA3A S1 One - NA4A S35 the the5B S25 - NA6B S25 Bayi the7C S11 A NA8C S11 + -Merge (W1, w2, by = C ("NAME","SCHOOL","CLASS"), all =T) NAME SCHOOL CLASS english.x MATHS english.y A S14 - NA NAA S1Ten - NA NAA S1 One - NA NAA S35 NA the theB S25 - the BayiC S11 A - +Merge (W1, w2, all =T, Incomparables ="A") ErrorinchMerge.data.frame (W1, w2, all =T, Incomparables ="A") :' Incomparables 'is supported only forMerging on a single columnmerge (W1, w2, all =T, by ="NAME", Incomparables ="A") NAME school.x class.x english.x school.y class.y MATHS english.y A S1Ten -<NA>NA NA NAA S14 -<NA>NA NA NAA S1 One -<NA>NA NA NAA <NA>NA NAS35 the theB S25 -S25 the BayiC S11 AS11 - +
Horizontal Merge
ID<-c (1,2,3,4)name<-c ("Jim","Tony","Lisa","Tom")score<-c ( the, A, +, +)Student1<- data. Frame(ID,name) Student2<- data. Frame(ID,score) total_student<-merge (student1,student2,by="ID")total_student
Of course merge can also use vertical merge
merge(data1,dadta2,all=T)
Merge vertically
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 Language-merge and Rbind