[R] Matched pairs with two data frames

Udo ukoenig at med.uni-marburg.de
Sun Apr 13 20:41:00 CEST 2008


Hi,
I have a frame "treat" and want to find matched pairs in the data frame
"control". In the matched (combined) data frame there should be two
variables (0/1),indicating the "source" of the data (treat or control),
so that it is possibe to set a "filter" (extraxt/select data).

#Here are the dataframes (my real data frames have many variables)
treat <- data.frame(age=c(1,1,2,2,2,4),
                    school=c(10,10,20,20,20,11),
                    out1=c(9.5,2.3,3.3,4.1,5.9,4.6))
control <- data.frame(age=c(1,1,1,1,3,2),
                      school=c(10,10,10,10,33,20),
                      out2=c(1.1,2,3.5,4.9,5.2,6.5))
print(treat)
print(control)

matched.data.frame <- ?????? #Match "treat" "control" by age school


#My SPSS syntax would be similar to this:
MATCH FILES FILE="treat" /IN=fromtreat
  /FILE="control" /IN=fromcontrol
  /BY age school.
SELECT IF fromdad AND fromfam. #select data, set filter

The /IN= option creates a 0/1 variable that indicates the
"source" of the data

The resultand matched data frame should have
the following structure:
age   school    out1    out2  fromtreat     fromcontrol
1	10	9.5	1.1  	1		1
1	10	2.3	2.0  	1		1
....
4	11	4.6	NA   	1		0
3	33	NA	5.2	0		1
....

I tried "which" and "match", but I failed and was unlucky looking
at the help/archive. Merge doesn´t do the job, because it makes
all possible matches.


Thank´s for any help!
Udo


--------------------------------------------
Udo K    N     G
      Ö     I

Clinic for Child an Adolescent Psychiatry
Philipps University of Marburg / Germany



More information about the R-help mailing list