[R] Newbie problem: Vectorizing a minimum function with constraints

Oliver Faulhaber oliverfaulhaber at gmx.de
Sat Feb 3 00:14:49 CET 2007


Sorry, if this question is rather basic, but being a newbie I still have 
problems to think in the "R way".

My problem is as follows:

- I have a data frame X with stock prices X$Price and corresponding 
dates X$Date.

- Now I want to get for each date x in X$Date the index z, such that
      z = min (a | X$Date(a)>x and X$Price(a)>Price(x)

To put it simple, I am looking for the first time in the future at which 
the stock price is higher than today's stock price. If such a time does 
not exist the output should be N.A.

An example input:

Index X$Price X$Date
1     20      2000-01-01
2     25      2000-01-02
3     15      2000-01-03
4     20      2000-01-04

The output should be

Index z
1     2
2     N.A.
3     4
4     N.A.

I don't have any idea how to program this in an elegant way in R. Any 
help is thus highly appreciated.

Thanks in advance
Oliver

-- 
Oliver Faulhaber (Dipl.-Math. oec., Aktuar DAV)
oliverfaulhaber at gmx.de · www.oliverfaulhaber.de



More information about the R-help mailing list