Dear all r-users, Suppose I have two data frame: A 1 3 4 5 2 and B 5 6 3 5 Now I want combine this two data frames without losing any value from either data frame. More precisely I want to see A B 1 5 3 6 4 3 5 5 2 NA I tried with cbind function but failed, as it only works when two data frames have equal number of rows. Can anyone suggest me any code that can be used for any data set? Thanks and regards [[alternative HTML version deleted]]
On Mon, 2006-06-12 at 14:44 +0530, Arun Kumar Saha wrote:> Dear all r-users, > > Suppose I have two data frame: > > A > 1 > 3 > 4 > 5 > 2 > > and > > B > 5 > 6 > 3 > 5 > > Now I want combine this two data frames without losing any value from either > data frame. More precisely I want to see > > A B > 1 5 > 3 6 > 4 3 > 5 5 > 2 NA > > I tried with cbind function but failed, as it only works when two data > frames have equal number of rows. Can anyone suggest me any code that can be > used for any data set?You can use merge() to do this. merge() will perform SQL-like joins on the two data frames. In this case, we are going to merge A and B, telling merge() to use the rownames in the two data frames as the basis for matching rows ('by' argument). We also tell it to include all rows ('all' argument) in case some (or all) of the rownames do not match between the two. Finally, we take the result of the operation, which by default will include an additional first column of the rownames themselves and we delete that column.> merge(A, B, all = TRUE, by = "row.names")[, -1]A B 1 1 5 2 3 6 3 4 3 4 5 5 5 2 NA See ?merge for more information. There are also many variations on its use that you will find in the list archives. HTH, Marc Schwartz
Hi I would recommend to use list instead how do you know that the result shall not be A B 1 NA 3 5 4 6 5 3 2 5> A<-1:5 > B<-4:7 > L<-list(A,B) > L[[1]] [1] 1 2 3 4 5 [[2]] [1] 4 5 6 7 If you insist on data frame you has to tell the program which cells are to be empty or if you have common column(s) you can use merge> CCstate psu weight 1 A. P. Urban 0 2 Mah. Rural 0 3 W.B. Rural 0 4 Ass. Rural 0 5 M. P. Urban 0 6 A. P. Urban 0> CC1state psu aaa 1 A. P. Urban 1.3 2 A. P. Rural 1.2 3 M. P. Urban 0.8>merge(CC,CC1, all=T) state psu weight aaa 1 A. P. Rural NA 1.2 2 A. P. Urban 0 1.3 3 A. P. Urban 0 1.3 4 Ass. Rural 0 NA 5 M. P. Urban 0 0.8 6 Mah. Rural 0 NA 7 W.B. Rural 0 NA HTH Petr On 12 Jun 2006 at 14:44, Arun Kumar Saha wrote: Date sent: Mon, 12 Jun 2006 14:44:14 +0530 From: "Arun Kumar Saha" <arun.kumar.saha at gmail.com> To: "r-help at stat.math.ethz.ch" <R-help at stat.math.ethz.ch> Subject: [R] Combinig two data frames> Dear all r-users, > > Suppose I have two data frame: > > A > 1 > 3 > 4 > 5 > 2 > > and > > B > 5 > 6 > 3 > 5 > > Now I want combine this two data frames without losing any value from > either data frame. More precisely I want to see > > A B > 1 5 > 3 6 > 4 3 > 5 5 > 2 NA > > I tried with cbind function but failed, as it only works when two data > frames have equal number of rows. Can anyone suggest me any code that > can be used for any data set? > > Thanks and regards > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.htmlPetr Pikal petr.pikal at precheza.cz