I am doing research on analyst recommendations on Finnish stock market, using Can Investors Profit from the Prophets? Security Analyst Recommendations and Stock Returns by Barber etc. as reference paper. I have daily analyst recommendations data, and individual stock returns data for finnish companies for time period 2000-2020. Stocks are divided into portfolios 1-5 based on their average analyst recommendation, and portfolios are rebalanced daily. What kind of R code should be used to get the portfolios to rebalance daily, and get the portfolio daily return to be equal to the average of the daily individual stocks returns that are in the portfolio?