基于列R中的多个条件构造一个循环 [英] Construct a loop based on multiple conditions in a column R

查看:33
本文介绍了基于列R中的多个条件构造一个循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a df attached and I would like to create a loop that would apply a specific sequence based on conditions in column "x9". I would like to be able to set the sequence myself so I can try different sequences for this data frame, I will explain more below.

对于算法,我有一个 df 得失.在获胜的第一个实例中,我想取"x9" 中的值并将其除以序列值.我想继续遍历序列值,直到实现损失为止.一旦丢失,序列将重新开始.

I have a df of losses and wins for an algorithm. On the first instance of a win I want to take the value in "x9" and divide it by the sequence value. I want to keep iterating through the sequence values until a loss is achieved. Once a loss is achieved the sequence will restart.

风险控件是我尝试创建的列,它从"x9" 中获取值,然后将其除以序列值.我希望能够更改序列值.

Risk control is the column I am attempting to create, it takes values from "x9" and divides them by the sequence value. I want to have the ability to alter the sequence values.

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

In short I need assistance in:

  1. 构建适用于我的df的序列,希望能够对其进行更改以尝试不同的序列;
  2. 取"x9"中的值.并创建一个将应用序列值集的新列.该序列采用"x9"中的值.并除以序列号;
  3. 构造一个循环遍历整个df的循环,以将其应用于所有值.

任何人都能提供的帮助/见解,我将不胜感激.

I would appreciate any help / insight anyone can provide.

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.00", 
"$478.69", "$0.00", "$320.45", "$0.00", "$503.70", "$0.00", "$609.30", 
"$0.00", "$478.19"), x10 = c("$30,000.00", "$30,478.69", "$30,478.69", 
"$30,799.14", "$30,799.14", "$31,302.84", "$31,302.84", "$31,912.14", 
"$31,912.14", "$32,390.33"), `Risk Control` = c(NA, "$478.69", 
NA, "$320.45", NA, "$251.85", NA, "$304.65", NA, "$159.40"), 
    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_character", 
    "collector")), x10 = structure(list(), class = c("collector_character", 
    "collector")), `Risk Control` = structure(list(), class = c("collector_character", 
    "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"))

推荐答案

也许有更好的方法,但是我相信以下函数可以解决问题.它有两个参数,一个要处理的向量 x 和一个序列 Seq .返回值是问题中描述的风险控制.

Maybe there are better ways but I believe the following function does what the question asks for. It takes two arguments, a vector x to be processed and a sequence Seq. The return value is the risk control described in the question.

constructRisk <- function(x, Seq){
  stopifnot(length(x) > 0)
  stopifnot(length(Seq) > 0)
  n <- length(x)
  m <- length(Seq)
  y <- numeric(n)
  iSeq <- 1L
  for(i in seq_len(n)){
    y[i] <- x[i]/Seq[iSeq]
    if(!is.na(y[i])){
      if(y[i] < 0) iSeq <- 0L
    }
    iSeq <- iSeq + 1L
    if(iSeq > m) iSeq <- 1L
  }
  y
}

请注意,由于发布的数据具有带有美元符号的 x9 列,因此属于"character" 类,因此下面的测试针对的是 X9 .如前所述,风险控制栏也是如此.

Note that since the posted data has column x9 with dollar signs and is, therefore, of class "character", the test below is on a numeric version of it, X9. And the same goes for the risk control column, as posted.

X9 <- as.numeric(sub("\\$", "", df1$x9))
RskCntr <- as.numeric(sub("\\$", "", df1$`Risk Control`))

RC <- constructRisk(X9, df1$Sequence)

all.equal(RskCntr, RC)
#[1] "Mean relative difference: 2.091175e-05"
all.equal(RskCntr, round(RC, 2))
#[1] TRUE

这篇关于基于列R中的多个条件构造一个循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆