簡體   English   中英

根據另一列R的條件創建function

[英]Create function based on condition of another column R

我附加了一個 df,我想創建一個循環,該循環將根據“x9”列中的條件應用特定序列(由用戶在 R 中設置)。 我希望能夠自己設置序列,這樣我就可以為這個數據框嘗試不同的序列,我將在下面解釋更多。

我有一個算法的損失和勝利的 df。 在獲勝的第一個實例中,我想獲取“x9”中的值並將其除以序列值。 我想繼續遍歷序列值,直到出現損失。 一旦發生損失,序列將重新開始,具體而言,當“x9”<0 時。

我想在我的示例“風險控制”和“序列”中創建兩列。 理想情況下,我希望 function 遍歷整個數據框,以便我可以將列“x9”與“風險控制”進行比較。

樣本數據:

structure(list(x1 = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10), x2 = c("2016.01.04 01:05", 
"2016.01.04 01:12", "2016.01.04 01:13", "2016.01.04 01:17", "2016.01.04 01:20", 
"2016.01.04 01:23", "2016.01.04 01:25", "2016.01.04 01:30", "2016.01.04 01:31", 
"2016.01.04 01:59"), x3 = c("buy", "close", "buy", "close", "buy", 
"close", "buy", "t/p", "buy", "close"), x4 = c(1, 1, 2, 2, 3, 
3, 4, 4, 5, 5), x5 = c(8.46, 8.46, 8.6, 8.6, 8.69, 8.69, 8.83, 
8.83, 9, 9), x6 = c(1.58873, 1.58955, 1.5887, 1.58924, 1.58862, 
1.58946, 1.58802, 1.58902, 1.58822, 1.58899), x7 = c(1.57873, 
1.57873, 1.5787, 1.5787, 1.57862, 1.57862, 1.57802, 1.57802, 
1.57822, 1.57822), x8 = c(1.58973, 1.58973, 1.5897, 1.5897, 1.58962, 
1.58962, 1.58902, 1.58902, 1.58922, 1.58922), x9 = c(0, 478.69, 
0, 320.45, 0, 503.7, 0, 609.3, 0, 478.19), x10 = c(30000, 30478.69, 
30478.69, 30799.14, 30799.14, 31302.84, 31302.84, 31912.14, 31912.14, 
32390.33), `Risk Control` = c(NA, 478.69, NA, 320.45, NA, 251.85, 
NA, 304.65, NA, 159.3966667), ...12 = c(NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA), Sequence = c(NA, 1, NA, 1, NA, 2, NA, 2, NA, 
3)), row.names = c(NA, -10L), class = c("tbl_df", "tbl", "data.frame"
), spec = structure(list(cols = list(x1 = structure(list(), class = c("collector_double", 
"collector")), x2 = structure(list(), class = c("collector_character", 
"collector")), x3 = structure(list(), class = c("collector_character", 
"collector")), x4 = structure(list(), class = c("collector_double", 
"collector")), x5 = structure(list(), class = c("collector_double", 
"collector")), x6 = structure(list(), class = c("collector_double", 
"collector")), x7 = structure(list(), class = c("collector_double", 
"collector")), x8 = structure(list(), class = c("collector_double", 
"collector")), x9 = structure(list(), class = c("collector_double", 
"collector")), x10 = structure(list(), class = c("collector_double", 
"collector")), `Risk Control` = structure(list(), class = c("collector_double", 
"collector")), ...12 = structure(list(), class = c("collector_logical", 
"collector")), Sequence = structure(list(), class = c("collector_double", 
"collector"))), default = structure(list(), class = c("collector_guess", 
"collector")), delim = ","), class = "col_spec"))

在此處輸入圖像描述

簡而言之,我需要以下方面的幫助:

1.Constructing a sequence to apply to my df,希望能夠改變這個序列來嘗試不同的序列;

2.在“x9”中獲取值並創建一個將應用序列值集的新列。 該序列取“x9”中的值並將其除以序列號

3.構造一個循環以遍歷整個 df 以將其應用於 dataframe 的所有值。

在上面的示例中,我手動創建了“風險控制”和示例“序列”。 示例中的順序是 1,1,2,2,3,3,4。 示例中的序列在迭代到下一個數字之前使用每個數字兩次。 一旦在“x9”中出現損失,序列就會重新開始。

對於此 function 和循環,我將不勝感激。 謝謝

僅從輸入數據開始(不需要的列)

df1 <- df %>% select(1:10)

將此數據減少為只有 x9 不為零的數據這可能不是有意的,用戶可能更喜歡關閉 x3 事件,但希望是說明性的。

df1 <- df1 %>% filter(x9 != 0)

啟動 seq 列並插入虛擬數據。

df1$seq <- c(1, NA, 1, NA, NA)

填寫,感謝 Allan Cameron 對我的帖子鏈接的回答

df1$seq <- unlist(sapply(diff(c(which(!is.na(df1$seq)), nrow(df1) + 1)), seq))

應用用戶規則 2:

df1$risk_control <- df1$x9 / df1$seq

# A tibble: 5 x 12
     x1 x2            x3       x4    x5    x6    x7    x8    x9    x10   seq risk_control
  <dbl> <chr>         <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>  <dbl> <int>        <dbl>
1     2 2016.01.04 0~ close     1  8.46  1.59  1.58  1.59  479. 30479.     1         479.
2     4 2016.01.04 0~ close     2  8.6   1.59  1.58  1.59  320. 30799.     2         160.
3     6 2016.01.04 0~ close     3  8.69  1.59  1.58  1.59  504. 31303.     1         504.
4     8 2016.01.04 0~ t/p       4  8.83  1.59  1.58  1.59  609. 31912.     2         305.
5    10 2016.01.04 0~ close     5  9     1.59  1.58  1.59  478. 32390.     3         159.

如果需要,可以將其與原始數據重新組合:

df2 <- dplyr::left_join(df[, -c(11:13)], df1)

# A tibble: 10 x 12
      x1 x2           x3       x4    x5    x6    x7    x8    x9    x10   seq risk_control
   <dbl> <chr>        <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>  <dbl> <int>        <dbl>
 1     1 2016.01.04 ~ buy       1  8.46  1.59  1.58  1.59    0  30000     NA          NA 
 2     2 2016.01.04 ~ close     1  8.46  1.59  1.58  1.59  479. 30479.     1         479.
 3     3 2016.01.04 ~ buy       2  8.6   1.59  1.58  1.59    0  30479.    NA          NA 
 4     4 2016.01.04 ~ close     2  8.6   1.59  1.58  1.59  320. 30799.     2         160.
 5     5 2016.01.04 ~ buy       3  8.69  1.59  1.58  1.59    0  30799.    NA          NA 
 6     6 2016.01.04 ~ close     3  8.69  1.59  1.58  1.59  504. 31303.     1         504.
 7     7 2016.01.04 ~ buy       4  8.83  1.59  1.58  1.59    0  31303.    NA          NA 
 8     8 2016.01.04 ~ t/p       4  8.83  1.59  1.58  1.59  609. 31912.     2         305.
 9     9 2016.01.04 ~ buy       5  9     1.59  1.58  1.59    0  31912.    NA          NA 
10    10 2016.01.04 ~ close     5  9     1.59  1.58  1.59  478. 32390.     3         159.

暫無
暫無

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

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