[R-SIG-Finance] xts question: how to get previous row?

Joshua Ulrich josh.m.ulrich at gmail.com
Wed Jul 15 15:11:28 CEST 2009


Mark,

I think the lag function does what you're asking.  Please see ?lag.xts
and note the differences between lag.xts and other time-series lag
functions.

> merge(aapl,lag(aapl))
            aapl lag.aapl.
2007-04-20  90.97        NA
2007-04-23  93.51     90.97
2007-04-24  93.24     93.51
2007-04-25  95.35     93.24
2007-04-26  98.84     95.35
2007-04-27  99.92     98.84
2007-04-30  99.80     99.92
2007-05-01  99.47     99.80
2007-05-02 100.39     99.47
2007-05-03 100.40    100.39
2007-05-04 100.81    100.40
> merge(aapl,lag(aapl))['2007-04-30',2]
          lag.aapl.
2007-04-30     99.92

Best,
Josh
--
http://www.fosstrading.com



On Mon, Jul 13, 2009 at 6:02 AM, Mark Breman<breman.mark at gmail.com> wrote:
> Thank you Murali, that does it for me.
>
> 2009/7/13 <Murali.MENON at fortisinvestments.com>
>
>> This appears to work:
>>
>> > aapl <- as.xts(read.zoo(textConnection("2007-04-20,      90.97
>> + 2007-04-23,      93.51
>> + 2007-04-24,      93.24
>> + 2007-04-25,      95.35
>> + 2007-04-26,      98.84
>> + 2007-04-27,      99.92
>> + 2007-04-30,      99.80
>> + 2007-05-01,      99.47
>> + 2007-05-02,     100.39
>> + 2007-05-03,     100.40
>> + 2007-05-04,     100.81"), sep=","))
>>
>> > aapl[grep("2007-04-30", index(aapl)) - 1]
>>            [,1]
>> 2007-04-27 99.92
>>
>> Murali
>>
>> -----Original Message-----
>> From: r-sig-finance-bounces at stat.math.ethz.ch
>> [mailto:r-sig-finance-bounces at stat.math.ethz.ch] On Behalf Of Mark
>> Breman
>> Sent: 13 July 2009 07:57
>> To: r-sig-finance at stat.math.ethz.ch
>> Subject: [R-SIG-Finance] xts question: how to get previous row?
>>
>> I have a really basic question but I can't find an answer for it.
>>
>> Supose I have this xts timeseries called aapl:
>>           AAPL.Close
>> 2007-04-20      90.97
>> 2007-04-23      93.51
>> 2007-04-24      93.24
>> 2007-04-25      95.35
>> 2007-04-26      98.84
>> 2007-04-27      99.92
>> 2007-04-30      99.80
>> 2007-05-01      99.47
>> 2007-05-02     100.39
>> 2007-05-03     100.40
>> 2007-05-04     100.81
>>
>> and supose I have a reference to a row in this series (i.e.
>> aapl["2007-04-30"]), what is the easiest way to select the previous row
>> (i.e. the row with index 2007-04-27) from the series?
>>
>> Thanks,
>>
>> -Mark-
>>
>>         [[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.
>>
>
>        [[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.
>



More information about the R-SIG-Finance mailing list