[R] help with loop over data frame

Marc Schwartz marc_schwartz at comcast.net
Mon Feb 19 03:59:07 CET 2007


On Mon, 2007-02-19 at 13:38 +1100, Dr Remo Sammassimo wrote:
> Dear List,
> 
> This may be the fifth time Ive tried to send this to the list so apologies
> if there are multiple emails.
> 
> I need some help getting started with this problem. I have a data frame
> containing a year of daily stock prices in the following format:
> 
> Date       Open   High    Low    Close
> 1/15/2000   10      11      8     10
> 1/16/2000   12      12     10     11
> etc..
> 
> 
> I want to create a new data frame which shows only the rows where the column
> value "Open" for 'today' is higher than the column value "High" for the
> previous day (previous row). How do I loop over each day accessing values
> from different rows and columns, as is needed here?
> 
> I have tried 'if' statements but none have worked.
> 
> Any help appreciated.
> 
> Regards,
> Alf Sammassimo
> Melbourne,Australia

I think that this should do it.

Presuming that your data frame is called 'DF':

Rows <- which(sapply(seq(along = rownames(DF))[-1], 
              function(x) DF[x, "Open"] > DF[x - 1, "High"])) + 1

DF.New <- DF[Rows, ]


The first line sets up a sequence from 2:nrows(DF) and then loops over
those indices. The indices are passed as 'x' to the function, which
compares the current row (x) "Open" value with the prior row (x - 1)
"High" value. This returns TRUE or FALSE for each row compared.

If TRUE, which() then returns the index of the row plus 1, since we do
not want the first row. Those indices are assigned to 'Rows', which is
then used to subset 'DF' and create 'DF.New'.

Just using the data you have above:

> DF.New
       Date Open High Low Close
2 1/16/2000   12   12  10    11


See ?which, ?sapply and ?seq

HTH,

Marc Schwartz



More information about the R-help mailing list