I have two datasets. For each dataset, every two columns is a pair with *a as 1st column and *b as 2nd column.
E.g. df1, pair A1, df1$A1a = 1st column & df1$A1b = 2nd column.
Likewise, df2, pair B1, df2$B1a = 1st column & df2$B1b = 2nd column.
df1:
ID A1a A1b A2a A2b
1 2 3 2 3
2 3 1 2 1
3 1 3 1 2
4 2 2 3 3
5 1 2 2 1
df2:
ID B1a B1b B2a B2b
1 1 2 2 3
2 3 2 1 1
3 2 3 2 2
4 3 2 2 3
5 2 2 3 1
The final data (df3) should look like this:
ID C1a C1b C2a C2b
1 1 2 2 3
2 3 1 1 1
3 1 3 1 2
4 2 2 2 3
5 1 2 2 1
I would like to do the following:
- First, compare the 1st column of each pair between df1 and df2 and identify the lowest value. E.g. For ID=1, compare
df1$A1a= 2 withdf2$B1a= 1, sincedf2$B1ahas the lower value, mutate new columns with the pairs from df2. I.e.df3$C1a= 1,df3$C1b= 2. - If the 1st column of each pair is the same, then use 2nd column to determine which pairs of values to mutate new columns. E.g. for ID=2, 1st column shows
df1$A1a= 3 anddf2$B1a= 3, therefore use 2nd column to determine, sincedf1$A1b= 1 anddf2$B1b= 2, the pairs of values should come from df1. I.e.df3$C1a= 3 anddf3$C1b= 1. - If both the pairs from df1 and df2 are the same, just use those values. E.g. for ID=1, 1st column
df1$A2a= 2 anddf2$B2a= 2 are the same, and 2nd columndf1$A2b= 3 anddf2$B2b= 3 are the same, then new columns should bedf3$C1a= 2 anddf3$C1b= 3.
Hoping to automate the above so that the code automatically compares every pair from df1 with df2 so that I do not need to compare the pairs individually (e.g. do A1 and B1 first, then do A2 and B2, etc) but rather the code just repeats for every pair in the datasets. Thank you for any help!
Aucun commentaire:
Enregistrer un commentaire