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)
}
In R, array indexes start at 1 - the 1st element is at index 1. This is different than 0-based languages like C, Python, or Java where the first element is at index 0.
Everybody knows that R is an inferior programming language, because vector indices start from 1, whereas in real programming languages like C and Python, array indexing begins from 0.
R is a "platform for experimentation and research". Its aim is to enable "statisticians to use the full capabilities of such an environment" without rethinking the way they usually deal with statistics. So people use formulas to make regression models, and people start counting at 1.
1-based numbering is the computational idea of indexing an ordered data structure (e.g., a string or array) by starting with 1 instead of 0. For example, if is the string "ACGT", then is given by the symbol 'A' and is 'C'.
Vectors store the multiple elements of the same data type in a single variable. But unlike the array in C programming, vectors indexing in R language starts from '1' and not with '0'.
one based
> PricerData <- matrix(NA, 30000, ncol=1)
> tgtStart <- 29593
> for (i in tgtStart:tgtStart) PricerData[i, 1] <- 111
> print(PricerData[29592, 1])
[1] NA
> print(PricerData[29593, 1])
[1] 111
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With