r code- creating a function of nested if else statements

I have a large data set, dim(3000, 7). I have a series of nested if else statements which I will reuse throughout my code. The code has to loop over each row to populate the data frame called secro. I have already created the first column of secro, see below. I wanted to create and run a function that would store and run the nested if else's but I am having trouble setting it up and choosing the arguments. Below is part of the nested if else's, the vars are df and secro, the column number will change and it will change as it loops through. So any guidance would be awesome.

for(i in + lookback.len:nrow(df)) {
  if(is.na(df$2[i]) | is.na(df$2[i] - lookback.len)) {
  # True
  if(secro$A1[i] == df$2[i]){
    #True
    if(is.na(df$3[i]) | is.na(df$3[i] - lookback.len)) {
      # True
      secro$A2[i] <- "Zero"
     # False 
     } else{ 
       if(secro$A1[i] == df$3[i]){
       # True
       secro$A2[i] <- "Zero"
       # False
       } else{ .....

sample data frame=df and output data frame=secro


>  **df**                                       **secro**

     Date   1   2   3   4   5   6   7              Date     A1  A2   A3  A4  A5
10/30/2015  EF  SL  GL  NA  NA  NA  NA          10/30/2015  EF
11/2/2015   GL  EF  NA  NA  NA  NA  NA          11/2/2015   EF
11/3/2015   GL  ED  NA  NA  NA  NA  NA          11/3/2015   EF
11/4/2015   GL  ED  NA  NA  NA  NA  NA          11/4/2015   EF
11/5/2015   GL  ED  NA  NA  NA  NA  NA          11/5/2015   EF
11/6/2015   GL  ED  EF  SP  NA  NA  NA          11/6/2015   EF
11/9/2015   GL  ED  EF  SP  SL  NA  NA          11/9/2015   EF
11/10/2015  GL  ED  EF  EE  SP  SL  NA          11/10/2015  EF
11/11/2015  ED  GL  EF  EE  SP  NA  NA          11/11/2015  EF
11/12/2015  ED  GL  EF  SP  EE  NA  NA          11/12/2015  EF
11/13/2015  GL  ED  EF  EE  NA  NA  NA          11/13/2015  EF
11/16/2015  GL  EF  EE  ED  SL  NA  NA          11/16/2015  EF
11/17/2015  GL  EF  EE  ED  SL  SP  NA          11/17/2015  EF

-->

What are you trying to do? Can you present a smaller example? Right now the only thing you have shown is setting cells to the value "Zero".

Try to explain in plain English what you want to accomplish.

1 Like

Respond to @elmstedt's question with something like

Given a data frame x create a second data frame y with y[,1] = x[,1] and y[2:6] equal to method for assigning the variables

That permits discussion of f(x) = y where f is the composition of functions needed to produce y.

f may feature a case_when(), which is preferable to nested ifelse here.

This is part of a back test, where each row of data is a date and the columns are variables 1-7. After running algos which output symbols I am comparing those symbols to other symbols on previous dates and in other columns. The decision tree is quite large but a good chunk of it gets repeated 3 times throughout the full tree. Therefore I wanted to make it that part a function so not to repeat the code 3 times. But I'm having a hard time wrapping my head around making a function that will loop through every date, what the arguments to list, and how to pass them throughout the function code. Thank you for any advice and assistance.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.