[R] Lowest data level since DateX

Gabor Grothendieck ggrothendieck at gmail.com
Fri May 13 17:41:48 CEST 2005


On 5/13/05, Lapointe, Pierre <Pierre.Lapointe at nbf.ca> wrote:
> Hello,
> 
> I'm dealing with financial time series.  I'm trying to find out X in this
> sentence:
> The most recent close is the lowest level since X(date).
> 
> Here's an example of what I'm looking for:
> 
> library(fBasics)
> data(DowJones30)
> tail(DowJones30[,1:5],n=10)
> 
> I need to come up with a vector that would look like this
> 
> AA             AXP          T            ...
> 2000-12-21     2000-12-20   2000-12-29
> 
> i.e. the last date at which the stocks were trading at a lower level than
> the most recent closing.
> 
> I know it has to do with min/max, pmin/pmax, cummin/cummax or rev(), but I
> can't figure it out.

The following returns the last index whose value is less than the
last entry in vector x (or numeric(0) if none):

> x <- c(1,4,2,6,3)
> tail(which(x < tail(x,1)),1)
[1] 3
> x <- c(1,4,2,6,.5)
> tail(which(x < tail(x,1)),1)
numeric(0)




More information about the R-help mailing list