Re: [R] help with loop over data frame

2007-02-19 Thread Gabor Grothendieck
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   HighLowClose
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 [EMAIL PROTECTED] 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   HighLowClose
 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@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.


__
R-help@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.


[R] help with loop over data frame

2007-02-18 Thread Dr Remo Sammassimo
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   HighLowClose
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@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.


Re: [R] help with loop over data frame

2007-02-18 Thread Marc Schwartz
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   HighLowClose
 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  1011


See ?which, ?sapply and ?seq

HTH,

Marc Schwartz

__
R-help@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.