We have the following code in R (already stripped to the bone for debugging)
tgtStart = 29593
for (i in tgtStart:tgtStart)
{
PricerData[i, 1] = 111
}
print(PricerData[29592,1])
PricerData being a matrix constructed well beyond 29593 and pre filled with NAs.
The thing I do not understand is that PricerData[29592, 1] will be changed to 111, this code prints 111, not the NA we expected.
Just to make it a bit more elaborate, the code I ended with today was:
tgtStart = 29593
for (i in tgtStart:tgtStart)
{
print(PricerData[i,1])
print(PricerData[i-1,1])
PricerData[i, 1] = 111
print(PricerData[i,1])
print(PricerData[i-1,1])
}
print(PricerData[29592,1])
It will print:
[1] NA
[1] NA
[1] 111
[1] NA
[1] 111
All as expected till the end, why oh why is the element before our start changed? And why not in the loop, only after the closing of the for-loop (Oh and if we change the 29593 to 29596, all works as expected, me not comprehend.).
Also, I don't code R, normally I'm doing C++, I'm to understand that R is one-based on it's vectors, but please do not mind to cover the real basics. We use R version 2.4.1.5, x64
EDIT: Okay, it's clear that I cut too much from our code, so for you the problem went away, here is the full function, for reference, I do not understand the output it gives above
LoadPricer <- function(instrument, colo, simDate, sliceSize, startTime, endTime, pricerName, pricerSettings, returnEmptyMatrixIfFileNotFound = FALSE)
{
fileName <- paste ( simulatorOutputBasePath, instrument, '\\', colo, '\\', format(simDate, '%Y%m%d'), '\\', sliceSize, '\\P#', pricerName, '#', pricerSettings, '.csv', sep='')
firstSliceRequest = GetSlicesSinceMidnight(startTime, sliceSize)
lastSliceRequest = GetSlicesSinceMidnight(endTime, sliceSize)
PricerData = as.matrix(matrix(NA, nrow=(lastSliceRequest - firstSliceRequest + 1), ncol = 1))
if (file.exists(fileName))
{
# Load entire file
AllData = as.data.frame(read.csv(fileName, header = TRUE, colClasses=c("customTimeFormat","numeric")))
if (dim(AllData)[1] > 0)
{
firstSliceData = GetSlicesSinceMidnight(as.POSIXlt(AllData[1,1]), sliceSize)
lastSliceData = GetSlicesSinceMidnight(as.POSIXlt(AllData[nrow(AllData),1]), sliceSize)
if ( firstSliceData <= lastSliceRequest & lastSliceData >= firstSliceRequest )
{
tgtStart = max(1, firstSliceData - firstSliceRequest + 1)
tgtEnd = min(lastSliceRequest - firstSliceRequest + 1, lastSliceData - firstSliceRequest + 1)
srcStart = max(1, firstSliceRequest - firstSliceData + 1)
srcEnd = min(lastSliceData - firstSliceData + 1, srcStart + tgtEnd - tgtStart)
#PricerData[as.integer(tgtStart):as.integer(tgtEnd),1] = AllData[as.integer(srcStart):as.integer(srcEnd),2]
for (i in tgtStart:tgtStart)
{
PricerData[i, 1] = 111# as.matrix(AllData[srcStart+i-tgtStart , 2])
}
}
}
}
else
{
if (returnEmptyMatrixIfFileNotFound) PricerData = matrix(NA, nrow=0, ncol=2)
print(paste('WARNING: Unable to load Pricer! File:', fileName))
}
return (PricerData)
}