Skip to content
Prev 109727 / 398500 Next

help with loop over data frame

On Mon, 2007-02-19 at 13:38 +1100, Dr Remo Sammassimo wrote:
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:
Date Open High Low Close
2 1/16/2000   12   12  10    11


See ?which, ?sapply and ?seq

HTH,

Marc Schwartz