根据另一列R的条件创建函数

时间:2020-11-12 18:02:53

标签: r function loops

我附加了一个df,并且我想创建一个循环,该循环将根据列“ x9”中的条件应用特定的序列(由用户在R中设置)。我希望自己能够设置序列,以便可以对此数据帧尝试不同的序列,我将在下面进行解释。

对于算法,我输赢了df。在第一次获胜的情况下,我想取“ x9”中的值并将其除以序列值。我想继续遍历序列值,直到实现损失。一旦达到损失,则序列将在“ x9” <0明确时重新开始。

我想在示例“风险控制”和“序列”中创建两列。理想情况下,我希望该函数遍历整个数据帧,以便可以将“ 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"))

enter image description here

简而言之,我需要以下方面的帮助:

1。构造一个序列以应用于我的df,希望能够更改此序列以尝试不同的序列;

2。在“ x9”中获取值,并创建一个新列,该列将应用设置的序列值。序列将“ x9”中的值除以序列号

3。构造一个循环遍历整个df的循环,以将其应用于数据帧的所有值。

在上面的示例中,我手动创建了“风险控制”和示例“序列”。示例中的顺序是1,1,2,2,3,3,4。样本中的序列使用每个数字两次,然后迭代到下一个数字。一旦在“ x9”中出现损失,该序列就会重新开始。

对于此功能和循环的任何帮助,我将不胜感激。谢谢

1 个答案:

答案 0 :(得分:1)

仅从输入数据开始(不需要列)

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

将此数据简化为仅x9不为零的数据 这可能不是故意的,用户可能更喜欢取消x3事件,但希望是说明性的。

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

启动seq列并插入虚拟数据。

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

填写,感谢艾伦·卡梅伦(Allan Cameron)对我的帖子link

的回答
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.