[R] paired samples, matching rows, merge()

Cecilia Carmo cecilia.carmo at ua.pt
Fri Aug 20 15:11:07 CEST 2010


The rule is not important to me. I'm selecting a sample 
that must have one important feature: the same number of 
obs from x and from y with the same k1.

Thanks
Cecília

Em Fri, 20 Aug 2010 08:28:24 -0400
  David Winsemius <dwinsemius at comcast.net> escreveu:
> 
> On Aug 20, 2010, at 6:44 AM, Cecilia Carmo wrote:
> 
>> Hi everyone!
>>
>> I'm matching two samples to create one sample that have
>> pairs of observations equal for the k1 variable. Merge() 
>>doesn't  
>> work because I dont't want to recycle the values.
> 
> When there is more than one possible match in either y 
>or x to a  possible match on k1 in the othr set of 
>values, is there some rule  that lets you determine which 
>one should be chosen. Your offered  solution suggests 
>that you think the order in the original data.frams  is a 
>proper rule, but why should we believe that rule is 
>anything  other than convenience?
> 
> -- 
> David.
>>
>> x <- data.frame(k1=c(1,1,2,3,3,5), 
>>k2=c(20,21,22,23,24,25))
>> x
>> y <- data.frame(k1=c(1,1,2,2,3,4,5,5), 
>>k2=c(10,11,12,13,14,15,16,17))
>> y
>> merge(x,y,by="k1")
>>  k1 k2.x k2.y
>> 1   1   20   10
>> 2   1   20   11
>> 3   1   21   10
>> 4   1   21   11
>> 5   2   22   12
>> 6   2   22   13
>> 7   3   23   14
>> 8   3   24   14
>> 9   5   25   16
>> 10  5   25   17
>>
>> I have a final dataframe with 10 rows, but I want it 
>>with 5 rows,  
>> like this:
>>  k1 k2.x k2.y
>> 1   1   20   10
>> 2   1   21   11
>> 3   2   22   12
>> 4   3   23   14
>> 5   5   25   16
>>
>> Thanks for any help.
>>
>> Cecília Carmo
>> (Universidade de Aveiro)
> 
> David Winsemius, MD
> West Hartford, CT
>



More information about the R-help mailing list