[R] help with loop over data frame

Gabor Grothendieck ggrothendieck at gmail.com
Mon Feb 19 12:31:27 CET 2007


Try this:

   DF[c(FALSE, tail(DF$Open, -1) > head(DF$High, -1)), ]

or using zoo objects just compare the Open to the reverse lag of the High.

Lines <- "Date       Open   High    Low    Close
1/15/2000   10      11      8     10
1/16/2000   12      12     10     11
1/17/2000   12      12     10     11
"
library(zoo)
z <- read.zoo(textConnection(Lines), header = TRUE, format = "%m/%d/%Y")
z[ z[, "Open"] > lag(z[, "High"],-1), ]



On 2/18/07, Dr Remo Sammassimo <remosammassimo at bigpond.com> 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
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



More information about the R-help mailing list