I have two data frames as follows:
df<-data.frame(
id=c("1-1","2-2","3-3","4-4","5-5","6-6"),
identifer=c(1,2,3,4,5,6),
key=c("A","B","C","D","E","F"),
product=c("productA","productB","productC","productD","productE","productF"),
ingredient=c("ingredientA","ingredientB","ingredientC","ingredientD","ingredientE","ingredientF"),
DF=c("Tablet","Powder","Suspension","System","Capsule","Capsule"))
df_2<-data.frame(
identifer=c(1,2,2,3,4,6),
key=c("A","B","B","C","D","F"),
product=c("productA","productB","productB","productCC","productDD","productFF"),
ingredient=c("ingredientA","ingredientBB","ingredientB","ingredientC","ingredientDD","ingredeintFF"),
DF=c("Tablet","Powder","Powder","Suspension","injection","tablet"),
Route=c("ORAL","INHALATION","INHALATION","topical","injecatable","oral")
)
I want to join these two datasets first on the following variables + create a new column called "match" that describes the join:
1) identifier,key, product, ingredient,DF
match="identifier,key, product, ingredient,DF"
Then, I want to join the REMAINING rows on these variables:
2)identifier, key, product, DF
match="identifier,key, product,DF"
Then the remaining rows from step 2 on these variables, so and so forth.
3) identifier, key, Ingredient, DF
4) identifier, key, DF
5) identifer, key, product, ingredient
7) identifer, key, product
8) identifer, key, ingredient
9) identifier, key
And I want to return the rows that do not have a match as well. I know how to do this stepwise but I'm wondering if there is an easier way to do this?
this is the expected output:
df_out<-data.frame(
identifer=c(1,2,3,4,5,6),
key=c("A","B","C","D","E","F"),
product_1=c("productA","productB","productC","productD","productE","productF"),
ingredient_1=c("ingredientA","ingredientB","ingredientC","ingredientD","ingredientE","ingredientF"),
DF_1=c("Tablet","Powder","Suspension","System","Capsule","Capsule"),
product_2=c("productA","productB","productCC","productDD",NA,"productFF"),
ingredient_2=c("ingredientA","ingredientB","ingredientC","ingredientDD",NA,"ingredeintFF"),
DF_2=c("Tablet","Powder","Suspension","injection",NA,"tablet"),
Route_2=c("ORAL","INHALATION",'topical',"injecatable",NA,"oral"),
Match=c("identifer+key+product+ingredient+DF","identifier+key+product+ingredient+DF","identifier+key+ingredient+DF","identifer+key","None","identifer+key+product+ingredient"))