Hi, I have two data frames each with 5 columns and different number of rows. some of the row names in one data frame are the same as the row names in the other. I want to be able to merge the two data frames to get a new data frame in which the duplicated row names are only shown once with the data for the rest of the columns used from the first data frame. Essentially, I want to make a union of the two data frames. I hope this question makes sense. Thanks
Hi all, I have two data frames each with 5 columns and different number of rows. some of the row names in one data frame are the same as the row names in the other. I want to be able to merge the two data frames to get a new data frame in which the duplicated row names are only shown once with the data for the rest of the columns used from the first data frame. for example: Pvals PD A 0.001 0.99 B 0.02 0.98 C 0.05 0.97 D 0.005 0.99 Pvals PD C 0.01 0.99 D 0.0002 0.98 E 0.05 0.97 F 0.02 0.99 Union Pvals PD A 0.001 0.99 B 0.02 0.98 C 0.05 0.97 D 0.005 0.99 E 0.05 0.97 F 0.02 0.99 Essentially, I want to make a union of the two data frames. I hope this question makes sense. Thanks
How about, uxy <- union(row.names(x), row.names(y)) ixy <- intersect(row.names(x), row.names(y)) rbind(x[is.element(row.names(x),uxy),], y[!is.element(row.names(y),ixy),]) Note, simple rbind'ing of the two frames changes common row.names. Gamal
Try either of these: rbind(DF1, DF2[setdiff(rownames(DF2), rownames(DF1)),]) rbind(DF1, DF2[!(rownames(DF2) %in% rownames(DF1)),]) On 9/19/06, Kartik Pappu <kartik.pappu at gmail.com> wrote:> Hi all, > > I have two data frames each with 5 columns and different number of > rows. some of the row names in one data frame are the same as the row > names in the other. I want to be able to merge the two data frames to > get a new data frame in which the duplicated row names are only shown > once with the data for the rest of the columns used from the first > data frame. > > for example: > > Pvals PD > A 0.001 0.99 > B 0.02 0.98 > C 0.05 0.97 > D 0.005 0.99 > > Pvals PD > C 0.01 0.99 > D 0.0002 0.98 > E 0.05 0.97 > F 0.02 0.99 > > Union > Pvals PD > A 0.001 0.99 > B 0.02 0.98 > C 0.05 0.97 > D 0.005 0.99 > E 0.05 0.97 > F 0.02 0.99 > > Essentially, I want to make a union of the two data frames. I hope > this question makes sense. > > Thanks > > ______________________________________________ > 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.html > and provide commented, minimal, self-contained, reproducible code. >
Kartik Pappu <kartik.pappu <at> gmail.com> writes:> Essentially, I want to make a union of the two data frames. I hope > this question makes sense.See merge(...), and have a look at R intro. Also check documentation for "Design" package. Anupam.