mardi 6 juillet 2021

If Else statement missing a test expression

I am trying to make a simple If Else statement, with using formula within the conditions like below ->

x="USD"

for (idx in 1:dim(Master_Dump_FXOptions)[[1]]){
  
    if (Master_Dump_FXOptions[idx,18] == x){
  (Scenario_Values_Options <- as.data.frame(apply(Master_Dump_FXOptions,1,function(each_row) as.numeric(each_row[16])*(((Currency_Ratios[as.numeric(each_row[20])])*Currency_Ratios[as.numeric(each_row[21])]-1)))))
  }
  else {
 (Scenario_Values_Options <- as.data.frame(apply(Master_Dump_FXOptions,1,function(each_row) as.numeric(each_row[17])*(((Currency_Ratios[as.numeric(each_row[20])])*Currency_Ratios[as.numeric(each_row[21])]-1))))*(-1))
  }
}

Issue is in my dataframe, first line is non USD (!=x), yet it still checks the If statement (considering as TRUE) and multiplies by 16th column rather than 17th (as per the else statement).

I am unable to figure out what is going wrong here. Any help would be highly appreciated. Thanks :)

Aucun commentaire:

Enregistrer un commentaire