0
votes

I have two datasets, the first dataset(DF1) is a treatment group, has ID, var1, var2, result four variables. ID and result are same for a person. but var1 and var2 are changing. Please notice each person has different row number. like ID1 has 3 rows, ID2 has only 2.

ID     var1              var2            result
 1      30                2013-11-23      2014-06-26
 1      30                2013-12-23      2014-06-26
 1      30                2014-1-23       2014-06-26
 2      60                2013-10-06      2014-05-10
 2      30                2014-01-6       2014-05-10

The second dataset (DF2)has same variable as ID, var1, var2. But it doesn't has result.

ID     var1              var2           
 a      30                2013-10-23      
 a      30                2013-11-23      
 a      30                2014-12-23       
 b      60                2013-10-06      
 b      30                2014-01-06       
 b      30                2014-02-03

My question is, what kind of method can I use to give each person in DF2 a result based on var1, var2 comparing to DF1? I think it may impossible for person in DF1 has totally same var 1 and var 2 as DF2..

Thank you so much for any help in advance!

1
your question is not entirely clear. can you give an example of your expected result? Also, take a look at ?mergeSymbolixAU

1 Answers

0
votes

Are you trying to match var1 and var2 in DF1 and DF2 and then assign a result to DF2 based on where there is a match? If so, this will work:

DF2$result <- NA
DF2[which((DF2$var1 %in% DF1$var1) & (DF2$var2 %in% DF1$var2)),'result'] <- DF1[which((DF1$var1 %in% DF2$var1) & (DF1$var2 %in% DF2$var2)),'result']

You will need to make sure var1, var2 and result are not factor variables, otherwise this might give erroneous results. If you need them to be factor, first convert them to numeric or character and then back to factor after running the above script.