0

The xts object, xts_IndData3 contains a PositionSize property that records the position size in a financial instrument and two other properties. NewEntrySize is >= 0. NewExitSize is <= 0. I put together the code below to avoid xts errors I've seen discussed elsewhere. The error I've tried to avoid has to do with referencing two different xts rowIDs within the same expression. Still I can't get the code below to work. Thanks in advance for assistance.

EDITED... I transformed the xts object to a data frame. Here's the data and new code with the problem doing what I need to accomplish clearer.

> str(TestData)
'data.frame':   12 obs. of  5 variables:
 $ DateTime                 : Date, format: "2012-07-27" "2012-07-27" "2012-07-27" ...
 $ NewEntrySize             : int  0 0 0 1 0 0 0 0 0 0 0 0...
 $ NewExitSize              : int  0 0 0 0 0 0 0 0 0 -1 0 0...
 $ PositionSize             : int  0 0 0 1 0 0 0 0 0 -1 0 0...
 $ DesiredResultPositionSize: int  0 0 0 1 1 1 1 1 1 0 0 0...
>    
>    PositionSizeLag <- 0
>    PositionSizeLag <- as.integer( PositionSizeLag )
>    TestData$PositionSize <- as.integer( TestData$PositionSize )
>    TestData$NewEntrySize <- as.integer( TestData$NewEntrySize )
>    TestData$NewExitSize  <- as.integer( TestData$NewExitSize  )
>    for (i in 1 : nrow( TestData )) {
+       TestData[i]$PositionSize <- PositionSizeLag              +
+                                    TestData[i]$NewEntrySize +
+                                    TestData[i]$NewExitSize
+       PositionSizeLag <- TestData[i]$PositionSize
+    }
Error in `$<-.data.frame`(`*tmp*`, "PositionSize", value = numeric(0)) : 
  replacement has 0 rows, data has 12
> 

I transformed the data frame to an xts object. Here's the output of a dput(head(xts_TestData)) on the xts version of the object.

> dput(head(xts_TestData))
structure(c("2012-07-27", "2012-07-27", "2012-07-27", "2012-07-27", 
"2012-07-27", "2012-07-27", "0", "0", "0", "1", "0", "0", "0", 
"0", "0", "0", "0", "0", "0", "0", "0", "1", "0", "0", "0", "0", 
"0", "1", "1", "1"), class = c("xts", "zoo"), .indexCLASS = "Date", tclass = "Date", .indexTZ = "UTC", tzone = "UTC", index = structure(c(1343347200, 
1343347200, 1343347200, 1343347200, 1343347200, 1343347200), tzone = "UTC", tclass = "Date"), .Dim = c(6L, 
5L), .Dimnames = list(NULL, c("DateTime", "NewEntrySize", "NewExitSize", 
"PositionSize", "DesiredResultPositionSize")))
> 
user1981673
  • 35
  • 1
  • 6
  • Do you have any `NA`s. If you made this [reproducible](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example), we could tell you exactly what the problem is, and probably come up with something that's orders of magnitude faster than using a for loop. – GSee Aug 16 '13 at 11:48
  • You "transformed the xts object to a data frame"?! Why? – GSee Aug 16 '13 at 12:51
  • Please add the output of `dput(head(xts_IndData3))` to your question – GSee Aug 16 '13 at 13:00

1 Answers1

1

You can't mix types in an xts object like you can in a data.frame, so all your as.integer calls do not change the xts object. There is also no need to have a DateTime column in an xts object, since that's accounted for by the index attribute.

You're getting errors with the code below because of your odd combination of [ and $. It's better to subset by column first (using $), then by row (using [).

PositionSizeLag <- 0
for (i in 1 : nrow( xts_TestData )) {
   xts_TestData$PositionSize[i] <- PositionSizeLag +
       xts_TestData$NewEntrySize[i] + xts_TestData$NewExitSize[i]
   PositionSizeLag <- xts_TestData$PositionSize[i]
}

And the error in the code in your question (using a data.frame) is because TestData[i] returns a column of the data.frame, and subsetting a data.frame by $ also returns a column. If you want to return a row of a data.frame, you need TestData[i,] (note the comma).

Joshua Ulrich
  • 173,410
  • 32
  • 338
  • 418