I write a for loop in order to use getDividends() function from quantmod library. I want to read the symbol and the date of buy from a dataframe and use them to download the dividends from a specific year
this is my tibble "div_dummy"
symbol shares date year_2021
symbol | shares | date | year_2021 | |
---|---|---|---|---|
1 | ABT | 5.00 | 2022-02-18 | 5.894309 |
2 | ABBV | 5.00 | 2021-09-01 | 5.894309 |
3 | AAPL | 5.00 | 2021-04-30 | 5.894309 |
4 | KO | 5.00 | 2022-02-18 | 5.894309 |
5 | MDLZ | 5.00 | 2021-09-01 | 5.894309 |
6 | CRM | 5.00 | 2022-02-18 | 5.894309 |
for (i in 1:nrow(div_dummy)) {
symbol_dummy <- div_dummy[i,1]
date_dummy <- div_dummy[i,3]
div_dummy$year_2021 <- sum(getDividends(symbol_dummy,
src = "yahoo",
start = date_dummy,
to = "2021-12-31"))
}
in the column "year_2021" I expect to have the sum of all dividends for the year 2021 for each stock. Instead, the for loop return only the sum of the dividends of the last stock symbols "ISP.MI".
How can i fix it?