I am doing some accounting but then wonder if there is a better way to replace for loop (CPU intensive) for long list of items.
Suppose we have a transaction record like this:
transaction_data
Qty Date_purchase Price_purchase Date_sale Price_sale
5 01/11/2020 ??? 02/11/2020 ???
10 03/11/2020 ??? 05/11/2020 ???
5 03/11/2020 ??? 06/11/2020 ???
....
What I have to do is fill the ??? with the price according to the date in this list.
price_list
Date Price
01/11/2020 10
02/11/2020 15
03/11/2020 17
....
I have tried merge, join but got stuck at writing the code with tidyverse in R (Can switch to Python if code work faster).
transaction_data
%>% transaction_data$Price_purchase <- left_join(price_list, by = c('Date_purchase', 'Date')
%>% transaction_data$Price_sale <- left_join(price_list, by = c('Date_sale', 'Date') (not working)
Edit: all the solutions are all equal good, I wish I could choose all as correct answer