replace - R xts NextMethod replacement length error -


the xts object, xts_inddata3 contains positionsize property records position size in financial instrument , 2 other properties. newentrysize >= 0. newexitsize <= 0. put code below avoid xts errors i've seen discussed elsewhere. error i've tried avoid has referencing 2 different xts rowids within same expression. still can't code below work. in advance assistance.

edited... transformed xts object data frame. here's data , new code problem doing need 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  ) >    (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 data frame xts object. here's output of dput(head(xts_testdata)) on xts version of 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 xts object can in data.frame, as.integer calls not change xts object. there no need have datetime column in xts object, since that's accounted index attribute.

you're getting errors code below because of odd combination of [ , $. it's better subset column first (using $), then row (using [).

positionsizelag <- 0 (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 error in code in question (using data.frame) because testdata[i] returns column of data.frame, , subsetting data.frame $ returns column. if want return row of data.frame, need testdata[i,] (note comma).


Comments

Popular posts from this blog

assembly - 8086 TASM: Illegal Indexing Mode -

Java, LWJGL, OpenGL 1.1, decoding BufferedImage to Bytebuffer and binding to OpenGL across classes -

javascript - addthis share facebook and google+ url -