
I'm trying to calculate the current yield for MSFT using the most recent dividend and daily closing price of MSFT. I've tried a couple of approaches that don't work, any suggestions would be appreciated. 
 
1) I have downloaded both the dividends and the OHLCVAdjC data successfully. When I merge the data with fill=0.0 and export the merged data with write.csv, the date is replaced with the index from 1 to 52, first few lines:
 














MSFT.Open
MSFT.High
MSFT.Low
MSFT.Close
MSFT.Volume
MSFT.Adjusted
Divs

1
29.59
29.65
29.21
29.41
31721800
29.22
0

2
29.21
29.53
28.97
29.19
39520500
29
0
 
2) Divs contains the dividend date and the 1 dividend 0.20. Stock contains theOHLVAdjC data for Microsoft. I can print the dates using Divs[1,0] and Stock[1,0] but a comparison of those dates always results in an error:
 
> 
> for (j in 1:d) {
+ for (i in 1:n) {
+ print(c(Stock[i,0],Divs[j,0]))
+ if ((Stock[i,0]) == (Divs[j,0]))  Current_Div <- Divs[j,1]
+ Stock$MSFT.Adjusted[i]<-Current_Div * Divs_Per_Year
+ }
+ }
2012-08-01
2012-08-14
Error in if ((Stock[i, 0]) == (Divs[j, 0])) Current_Div <- Divs[j, 1] : 
  argument is of length zero
Execution halted

Code follows:
 
library(quantmod)
## Div frequency
Divs_Per_Year<-4
ticker<-"MSFT"
startDate=as.Date("2012-08-01")
endDate=as.Date("2012-10-13")
ticker
startDate
endDate
Divs<-getDividends(ticker, from = startDate, to = endDate)
d<-nrow(Divs)
d
Divs
Stock<-getSymbols.yahoo(ticker, from = startDate, to = endDate, src = "yahoo", verbose=TRUE, auto.assign=FALSE)
n<-nrow(Stock)
n
Stock_Divs<-merge(Stock,Divs,fill=0.0)
Stock_Divs
Divs_Per_Year
Current_Div<-Divs[1,1]
Current_Div
d<-nrow(Divs)
d
write.csv(Stock_Divs, file = "Stock_Divs.csv", append = FALSE, quote = TRUE, sep = " ")
for (j in 1:d) {
for (i in 1:n) {
print(c(Stock[i,0],Divs[j,0]))
if ((Stock[i,0]) == (Divs[j,0]))  Current_Div <- Divs[j,1]
Stock$MSFT.Adjusted[i]<-Current_Div * Divs_Per_Year
}
}
 
 
Frank
Chicago, IL 		 	   		  
	[[alternative HTML version deleted]]

