5

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)
}
Colander
  • 159
  • 1
  • 5
  • 3
    This works fine for me. I suspect you forgot to re-initialize `PricerData` after accidentally writing `111` to `PricerData[29592,1]`. – Joshua Ulrich Jan 15 '13 at 18:14
  • I updated with the full code, as you can see, PricerData is left alone. The snippet I posted with the 5 prints ran exact as that.... it this context – Colander Jan 15 '13 at 18:30
  • 1
    Why do you do `for (i in tgtStart:tgtStart)`? Why not just assign `PricerData[tgtStart, 1]`? Also, why don't you print `tgtStart` before the assignment? – Diego Basch Jan 15 '13 at 18:31
  • 2
    Look, the only way that that value will have changed is because you have inadvertently told R to alter it. You just need to figure out where. The information you've provided us so far isn't really going to make it easy for us to debug your code for you (which is off topic anyway). – joran Jan 15 '13 at 18:36
  • Why do you do for (i in tgtStart:tgtStart)? Why not just assign PricerData[tgtStart, 1]? : Legacy from the code before we started stripping and discovered this situation. I did print tgtStart in another attempt, it prints 29593 off course :) – Colander Jan 15 '13 at 18:37
  • If anything your code is becoming less reproducible. – GSee Jan 15 '13 at 18:45
  • Hum, I think you should make your example reproducible. See http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Roman Luštrik Jan 15 '13 at 18:56
  • Do you really need a matrix that has 29593 rows to illustrate your problem? Try it with one with 20 rows. Its much easier to print out to see what is happening... – Spacedman Jan 15 '13 at 19:52

2 Answers2

10

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
GSee
  • 48,880
  • 13
  • 125
  • 145
0

But you start with tgtStart=29593

So it will do i= 29593, therefore it will replace PricerData[29593, 1] = 111, not the PricerData[29592, 1]

> tgtStart = 29593
> for (i in tgtStart:tgtStart)
> {
>  PricerData[i, 1] = 111
> }
> print(PricerData[29592,1])
[1] NA
> print(PricerData[29593,1])
[1] 111
Gago-Silva
  • 1,873
  • 4
  • 22
  • 46