[R] merging data.frames of different length

Martin Batholdy batholdy at googlemail.com
Thu Jun 18 03:04:25 CEST 2009


I have tried to replicate the example on the help page;


x <- data.frame(category = sample(3, 10, r=TRUE), rnorm(10, 5, 2))
y <- data.frame(category = sample(3, 10, r=TRUE), rnorm(10, 8, 2))

merge(x, y, by = "category")



When I do that, I get a data.frame with 28 rows instead of 10.

What am I doing wrong?



Am 18.06.2009 um 02:42 schrieb David Winsemius:

> Red the help page for merge:
>
> ?merge
>
>
> On Jun 17, 2009, at 8:33 PM, Martin Batholdy wrote:
>
>> hi,
>>
>>
>> I have two data.frames each with two columns;
>>
>>
>> x1
>>
>> 1	4
>> 1	3
>> 1	6
>> 2	9
>> 2	2
>> 2	5
>> 3	6
>> 3	7
>> 3	4
>>
>>
>> x2
>>
>> 1	-3
>> 1	-7
>> 2	-3
>> 2	-2
>> 2	-8
>> 3	-1
>> 3	-2
>> 3	-1
>>
>> now I want to merge this data.frames to one data.frame.
>>
>> The problem is, that sometimes there is a different number of  
>> elements per category.
>> (like above x1 has 3 values for the value 1 in the first row, but  
>> x2 has only 2 values for the value 1 in the first row).
>>
>> Is there an easy way to merge this two data.frames by deleting the  
>> rows that only one data.frame "has".
>> In the example, that resulting data.frame would be the data.frame  
>> x1 and x2 except the row 3 of data.frame x1.
>>
>> thanks for any suggestions!
>>
>> ______________________________________________
>> R-help at 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.
>
> David Winsemius, MD
> Heritage Laboratories
> West Hartford, CT
>




More information about the R-help mailing list