[R-SIG-Finance] removing repeating values from xts series

Joshua Ulrich josh.m.ulrich at gmail.com
Wed Sep 15 16:06:41 CEST 2010


Ulrich,

Patrick's suggestion is a vectorized solution to your problem.  But it
won't work for xts objects because they are merged by time index
before the comparison.

You need to use lag:

> x <- xts(cbind(c(100,100,100,101,102,100),
+   1,c(101,101,101,102,102,101),1),
+   as.POSIXct("2010-01-01 09:00:01")+0:5)
> x[!c(FALSE,apply(lag(x)==x,1,all)[-1]),]
                    [,1] [,2] [,3] [,4]
2010-01-01 09:00:01  100    1  101    1
2010-01-01 09:00:04  101    1  102    1
2010-01-01 09:00:05  102    1  102    1
2010-01-01 09:00:06  100    1  101    1

Or you could use diff (as you suggest):

> x[!c(FALSE,apply(diff(x)==0,1,all)[-1]),]
                    [,1] [,2] [,3] [,4]
2010-01-01 09:00:01  100    1  101    1
2010-01-01 09:00:04  101    1  102    1
2010-01-01 09:00:05  102    1  102    1
2010-01-01 09:00:06  100    1  101    1

Best,
--
Joshua Ulrich
FOSS Trading: www.fosstrading.com



On Wed, Sep 15, 2010 at 4:33 AM, Ulrich Staudinger
<ustaudinger at gmail.com> wrote:
> I think diff and a logical operation on all four colums would help.
> I hoped I would find a ready function for ...
> Thanks ...
>
> On Wed, Sep 15, 2010 at 9:46 AM, Ulrich Staudinger
> <ustaudinger at gmail.com> wrote:
>> I want to compare
>>
>> y(t) with y(t-1)
>> where
>> t = 2... length(y)
>> y is an xts timeseries
>>
>>
>>
>> On Wed, Sep 15, 2010 at 9:33 AM, Patrick Burns <patrick at burns-stat.com> wrote:
>>> So you want to compare
>>>
>>> y[-1,]
>>>
>>> with
>>>
>>> y[-nrow(y),]
>>>
>>> I think.  And save the rows
>>> that aren't all equal.  Yes?
>>>
>>
>>
>>
>>
>> --
>> Ulrich Staudinger
>> activequant.org
>>
>
>
>
> --
> Ulrich Staudinger
> ustaudinger at activequant.org
> http://www.activequant.org
>
> _______________________________________________
> 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