簡體   English   中英

R-循環內:根據匹配條件,在下面的下一行設置變量

[英]R - Within Loop: On match criteria set variable next line below

我有一個循環,嘗試根據匹配條件對其進行編輯,該變量將在下面的下一行中設置。 現在,它的設置為signal == 1 ,它將存儲close.detrend.n11價格以供將來比較。 但是,我希望在signal == 1 ,在下close.detrend.n11存儲下一個close.detrend.n11價格。

如果我們采用這部分代碼:

state <- "off"
    for (i in 1:nrow(new.df)) { # loop through data
      if (state == "off") { # off state, loop does nothing until signal = 1
        if (new.df$signal[i] == 0) {
          next
        } else { # signal = 1 encountered
          comparison_price <- new.df$close.detrend.n11[i] # save current price for comparing
          n_comparisons <- 0              # keep track of how many comparisons
          state <- "on"                   # change state to "on"
        }

找到信號== 1的部分是} else { # signal = 1 close.detrend.n11 } else { # signal = 1接下來,它在與signal == 1相同的行上設置close.detrend.n11 ,將保存close.detrend.n11價格以供將來比較。

目標是在信號== 1上,將其設置在+1線BELOW信號== 1上的比較價格。然后環路將按預期發揮作用。

整個代碼在這里用於復制或其他研究目的,看起來像是可以使用的相當漂亮的代碼。

new.df <- data.frame(new.df,response=0)
input <- new.df$close.detrend.n11 # Input column for code
signal <- new.df$detrend.signal.n11
nlines <- 10 # Time Stop

state <- "off"
for (i in 1:nrow(new.df)) { # loop through data
  if (state == "off") { # off state, loop does nothing until signal = 1
    if (signal[i] == 0) {
      next
    } else { # signal = 1 encountered, if not a 0 it will do whats below...
      comparison_price <- input[i] # save current price for comparing
      n_comparisons <- 0              # keep track of how many comparisons
      state <- "on"                   # change state to "on"
    }
  } else if (state == "on") { # Above turned on as found signal = 1
    new.df$response[i] <- 1 
    if (input[i] > comparison_price & n_comparisons >= 1) { # found higher price
      state <- "off" # Turns off as found higer price, now does nothing until signal == 1
    } else if (n_comparisons == 10) { # Shut off maximum travel on higher prices
      state <- "off"  # Turns off as max limit reached
      n_comparisons <- 0   # Sets count to 0
      if (signal[i] == 1) {  # Find signal match
        comparison_price <- input[i] + 1 # Set price for comparing
        n_comparisons <- 0 # Sets count to 0
        state <- "off"    # Turn off 
      }
    } else if (n_comparisons == nlines) { # hit comparison limit
      state <- "off"  # Turns off as max limit reached
      n_comparisons <- 0   # Sets count to 0
      if (signal[i] == 1) { # Finds signal ==1
        comparison_price <- input[i] # Set price for comparing
        n_comparisons <- 0 # Set count to 0
        state <- "on" # Turn On
      }
    } else { # price less or equal to comparison price
      n_comparisons <- n_comparisons + 1
    }
  }
}

上面的代碼將產生如下輸出:

下面的new.df

  detrend.signal.n11 response close.detrend.n11
1                   0        0       0.002044539
2                   0        0      -0.022593487
3                   1        0      -0.031842265
4                   1        1      -0.065392575
5                   0        1      -0.043699817
6                   0        1      -0.014110718
7                   0        0      -0.021899531
8                   0        0      -0.013908376
9                   0        0      -0.019580252
10                  0        0       0.023034983
11                  0        0       0.014598769
12                  0        0       0.013928860
13                  0        0       0.008568669
14                  0        0       0.020220697
15                  0        0      -0.003770356
16                  0        0      -0.021588957
17                  0        0      -0.018637185
18                  0        0      -0.007193684
19                  0        0      -0.013691624
20                  0        0      -0.020903833
21                  1        0      -0.036922613
22                  0        1      -0.011845136
23                  0        1       0.001115208
24                  0        0       0.018121000
25                  0        0       0.027648296
26                  0        0       0.016920882
27                  0        0       0.006321574
28                  0        0      -0.012052026
29                  0        0      -0.017340348
30                  0        0      -0.029062592
31                  0        0      -0.012560974
32                  0        0      -0.029126952
33                  0        0      -0.026220867
34                  0        0       0.007337385
35                  0        0       0.009356641
36                  0        0       0.027851405
37                  0        0       0.044130597
38                  0        0       0.036630768
39                  0        0       0.028685373
40                  0        0       0.030813270
41                  0        0       0.020690203
42                  0        0       0.014865516
43                  0        0      -0.001644471
44                  0        0       0.011208823
45                  0        0       0.009698927

對不起,它是一個問題的龐然大物或一個瘋狂的例子。

主要問題是如何在循環內的初始比賽以下為+1行。

您可以在base R中嘗試這樣的操作

# Simulate `lead` in `dplyr`
future.detrend <- close.detrend.nll[2:nrow(df)]

# iterate through df, if signal ==1, keep future.detrend value, else NA
next.close.detrend.nll <- lapply(1:nrow(df), function(x) ifelse(df$detrend.signal.nll[x]==1, future.detrend[x], NA)) 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM