简体   繁体   中英

Creating A For Loop That Is Not Sequential in R

I have a situation in which my program is going through a data frame and finding the rows that meet certain criteria. It then subsets the information into a separate data frame. The trouble I have is that when using a for loop, I am getting overlapping data.

For example, if the first row meets my criteria for entry and the fourth row meets criteria for exit, I don't need it to also say that row 2 meets criteria for entry. I would like the loop to recommence on row 5.

Here is an example:

Ratio
0.87
0.88
0.86
0.90
0.92
0.93

Assume the criteria is find the first data point where the ratio is less than 0.9 and then find the next point that is greater than or equal to 0.9. In this case, I want row 1 as entry and row 4 as exit. I do not want the loop to then check row 2. The next step is to check row 5 and onward for the first data point below 0.9. Once it finds one, it then finds the next row with a data point greater than or equal to 0.9.

I am doing this now in a convoluted way which is causing other issues and is just sloppy.

for (i in 1:length(data$Ratio))
  {
    while (!i==1 && data$Date[i] <= ExitDate )
    {i = i+1}
...

This succeeds in increasing i but again, this is sloppy. I am sure there is a way in R to accomplish this in a more clean way, maybe even without a for loop.

Thank you for your help.

Solution I have implemented per Jake's suggestion:

for (i in 1:length(data$Ratio))
  { 

    if(data$Ratio[i] < ShortExit && data$Ratio[i-1] >= ShortExit
       || i==1 && data$Ratio[i] < ShortExit )

...

I preferred to stay with the loop structure than modify the data, however, I was able to get rid of the while loop by using Jake's idea of checking the current data point and the one prior to it. ShortExit is the threshold value I asked about. It is 0.9 in the example I gave. Thank you.

data$lag_Ratio <- c(NA, head(data$Ratio, -1))

entries <- data$Ratio < 0.9 & data$lag_Ratio >= 0.9 | is.na(data$lag_Ratio)
## [1]  TRUE FALSE FALSE FALSE FALSE FALSE

exits <- data$Ratio >= 0.9 & data$lag_Ratio < 0.9
## [1] FALSE FALSE FALSE  TRUE FALSE FALSE

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM