[R] Merging two data frames

arun smartpink111 at yahoo.com
Thu Mar 27 11:40:17 CET 2014


Hi,
Try:

A <- as.data.frame(matrix(1:20,byrow=TRUE,ncol=4))
B <- as.data.frame(matrix(21:40,byrow=TRUE,ncol=4))
 AB <- rbind(A,B)
#if the ?row.names of both datasets are from 1:nrow(dataset)
AB1 <- AB[order(as.numeric(c(row.names(A),row.names(B)))),]
#or
AB2 <- AB[c(matrix(seq(dim(AB)[1]),nrow=2,byrow=TRUE)),]
#or

AB3 <- AB[order(c(seq(dim(A)[1]),seq(dim(B)[1]))),]

identical(AB1,AB2)
#[1] TRUE
 identical(AB1,AB3)
#[1] TRUE
A.K.


I want to merge two data frames with approximately same number of rows. 
But the merging needs to be done in a special way. 
Suppose the two data frames are A and B. and Ai, Bi represents the ith row of the respective data frames. 
Then I want a new dataframe with the following rows: 

A1 
B1 
A2 
B2 
. 
. 
. 
. 
Ai 
Bi 
. 
. 
. 

How do i efficiently do it in R ? 

Thank you. 




More information about the R-help mailing list