[R-SIG-Finance] adding xts time series
Joshua Ulrich
josh.m.ulrich at gmail.com
Sun Aug 22 02:19:59 CEST 2010
Your issue is caused by xts merging objects before performing math operations.
> x <- .xts(1:10,1:10)
> x[10]-x[9] # empty xts object
Data:
integer(0)
Index:
NULL
> x[10]-lag(x)[10] # this aligns the index before subtracting
[,1]
1969-12-31 18:00:10 1
Be careful to note that your solution works because the "[[" function
does not return an xts object, so using "[[" may cause issues if you
use it when you do expect / need an xts object returned.
Best,
--
Joshua Ulrich
FOSS Trading: www.fosstrading.com
On Sat, Aug 21, 2010 at 4:58 PM, Nick Torenvliet
<nick.torenvliet at gmail.com> wrote:
> myPrediction[10,1]<- myPrediction[[10,1]]-myPrediction[[9,1]]
>
> phhhhhttt!!
>
> On Sat, Aug 21, 2010 at 5:28 PM, Nick Torenvliet
> <nick.torenvliet at gmail.com>wrote:
>
>> An obviously dumb question but I'm trying to add/subtract xts data...
>>
>> >myPrediction[10]<- myPrediction[10]-myPrediction[9]
>> Error in NextMethod(.Generic) : replacement has length zero
>>
>> >myPrediction[10,1]<- myPrediction[10,1]-myPrediction[9,1]
>> Error in NextMethod(.Generic) : replacement has length zero
>>
>> > myPrediction[10,1] <- (myPrediction[10,1]-myPrediction[9,1])
>> Error in NextMethod(.Generic) : replacement has length zero
>>
>> > is.xts(myPrediction)
>> [1] TRUE
>>
>> > myPrediction[10]
>> Nov 2009 9854.299
>>
>> > myPrediction[9]
>> Oct 2009 9713.752
>>
>> What is my issue here?
>>
>
> [[alternative HTML version deleted]]
>
> _______________________________________________
> R-SIG-Finance at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-sig-finance
> -- Subscriber-posting only. If you want to post, subscribe first.
> -- Also note that this is not the r-help list where general R questions should go.
>
More information about the R-SIG-Finance
mailing list