[R] how to 'stack' data frames?

Gabor Grothendieck ggrothendieck at gmail.com
Thu Jul 29 13:07:52 CEST 2010


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.



More information about the R-help mailing list