I have a couple of large dataframes of the following pattern(s), df1:
date cat value code
2022-07-10 A 0.6 12Az
2022-07-10 B 0.5 131x
2022-07-12 C 0.5 55tg
2022-07-18 A 0.1 fs12
2022-07-19 C 0.6 55tg
2022-07-19 B 0.6 zz12
df2:
date cat allocation code
2022-07-10 A 100 12Az
2022-07-10 B 450 131x
2022-07-12 C 220 55tg
2022-07-18 A 360 fs12
2022-07-19 C 221 55tg
2022-07-19 B 485 zz12
What I'd like to do is bring the 'allocation' column from df2 into df1 using something like a match function that matches the data from the 'date' and 'cat' columns.
I was hoping it would be as easy as writing something like:
df1$allocation <- df2$allocation[match(df1$date, df2$date & df1$cat, df2$cat)]
But unfortunately that just throws up errors for me. Does anyone have any advice on how to fix this?
CodePudding user response:
Sounds like a job for the dplyr::left_join()
function. It would be similar to
library(dplyr)
left_join(df1, df2, by=c("date"="date", "cat"="cat"))