Home > Software engineering >  An if statement returns a different variable on each branch, how can I assign to that variable using
An if statement returns a different variable on each branch, how can I assign to that variable using

Time:09-26

Consider

if(sample(2, 1) == 1) a <- 5 else b <- 5

This is repetitive. Is there any way to not have to write <- 5 twice?

CodePudding user response:

You may use assign -

assign(if(sample(2, 1) == 1) 'a' else 'b', 5) 
  • Related