I have two dataframes of different dimensions, call them df1 and df2. They both have columns of integers called Product and Output.
df2 has the unique column of numbers called ActualOne. I want to append all the values of ActualOne from df2 to df1, where the Product and Output values of df1 are jointly matched with those in df2. For Product,Output pairs not found in df2, ActualOne is populated by NAs.
Attempt #1: Use data.table
library(data.table)
setDT(df1)
setDT(df2)
df1[df2, ActualOne := i.ActualOne, on=c("Product", "Output")]
I am by no means an expert at data.table. I didn't think anything had to be reformatted and thought the temporary storing as a data.table was better.
I received the following error:
Error in
[.data.table
(df1, df2,:=
(ActualOne, i.ActualOne), : unused argument (on = c("Product", "Output"))
Attempt #2: Use a matching function
Off the top of my head something along the lines of:
df1$ActualOne <- df2[match(df1$Product,df2$Product) & match(df1$Output,df2$Output,"ActualOne"]
This results in the ActualOne column of df1 being populated by "ActualOne"
Thanks for your help.