简体   繁体   中英

R xts NextMethod replacement length error

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")))
> 

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM