If you want to avoid this kind of error and implement efficient code in R, you have to learn how to use function like lapply, apply and others *apply. There's an excellent post here
Here is my simplification of your code, not fully tested but it will give insight on how you can improve your code and correct some errors.
require(tseries)
require(zoo)
symbs <- c('XLF', 'XLE', 'XLU', 'SPY')
importData <- lapply(symbs, function(symb)
get.hist.quote(instrument= symb,
start = "2000-01-01",
end = "2013-07-15",
quote="AdjClose", provider = "yahoo",
origin="1970-01-01", compression = "m",
retclass="zoo"))
names(importData) <- symbs
monthlyPctChgs <- lapply(1:12, function(y)
lapply(importData,
function(x) diff(x, lag = y) / lag(x, lag = - y)))
tsPctChgs <- lapply(monthlyPctChgs, do.call, what = merge)
curBestLagPerfs <- lapply(tsPctChgs, function(y)
rollapplyr(y, 2,
function(x) x[2,which.max(x[1,])],
by.column=FALSE))
curBestLagPerfs <- do.call(merge, curBestLagPerfs)
names(curBestLagPerfs) <- month.abb
str(curBestLagPerfs)
## ‘zoo’ series from 2000-03-01 to 2013-06-03
## Data: num [1:160, 1:12] 0.09156 0.00933 -0.00229 -0.06095 -0.01496 ...
## - attr(*, "dimnames")=List of 2
## ..$ : NULL
## ..$ : chr [1:12] "Jan" "Feb" "Mar" "Apr" ...
## Index: Date[1:160], format: "2000-03-01" "2000-04-03" "2000-05-01" ...
Hope it will help