I have 2 data frames (A & B) with some common column names. A has 10 rows. B has 20 rows. How do I combine them so I end up with a data frame with 30 rows that only contains the common columns. I was trying 'merge' (Merge two data frames by common columns .....etc. ) but that is not giving me what I expect...> a <- iris > b <- iris > > c <-merge(a,b) > > NROW(a)[1] 150> NROW(c)[1] 152 Why is there only 152 rows and not 300? -- View this message in context: http://r.789695.n4.nabble.com/how-to-stack-data-frames-tp2306284p2306284.html Sent from the R help mailing list archive at Nabble.com.
Hi: Here's a *reproducible* example:> a <- data.frame(w = rnorm(10), x = rnorm(10), y = rnorm(10), z rnorm(10)) > b <- data.frame(w = rnorm(20), x = rnorm(20), x2 = rnorm(20), x3 rnorm(20)) > commonCols <- intersect(names(a), names(b)) > commonCols[1] "w" "x"> rbind(a[commonCols], b[commonCols])HTH, Dennis On Thu, Jul 29, 2010 at 3:53 AM, pdb <philb@philbrierley.com> wrote:> > I have 2 data frames (A & B) with some common column names. > > A has 10 rows. > B has 20 rows. > > How do I combine them so I end up with a data frame with 30 rows that only > contains the common columns. > > I was trying 'merge' (Merge two data frames by common columns .....etc. ) > but that is not giving me what I expect... > > > a <- iris > > b <- iris > > > > c <-merge(a,b) > > > > NROW(a) > [1] 150 > > NROW(c) > [1] 152 > > Why is there only 152 rows and not 300? > > > > -- > View this message in context: > http://r.789695.n4.nabble.com/how-to-stack-data-frames-tp2306284p2306284.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help@r-project.org 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. >[[alternative HTML version deleted]]
On Thu, Jul 29, 2010 at 6:53 AM, pdb <philb at philbrierley.com> wrote:> > I have 2 data frames (A & B) with some common column names. > > A has 10 rows. > B has 20 rows. > > How do I combine them so I end up with a data frame with 30 rows that only > contains the common columns. > > I was trying 'merge' (Merge two data frames by common columns .....etc. ) > but that is not giving me what I expect... > >> a <- iris >> b <- iris >> >> c <-merge(a,b) >> >> NROW(a) > [1] 150 >> NROW(c) > [1] 152 > > Why is there only 152 rows and not 300? >Because rows 102 and 143 are the same:> iris[c(102, 143),]Sepal.Length Sepal.Width Petal.Length Petal.Width Species 102 5.8 2.7 5.1 1.9 virginica 143 5.8 2.7 5.1 1.9 virginica so row 102 gets joined to both 102 and 143 and row 143 gets joined to both 102 and 143 and all other rows get joined to themselves giving 2 + 2 + 148 = 152 rows.