If an operator with multiple actions in R

I would like to write an if statement like this:

a=5 b=2 la<-function(a,b){ if(a>3){a} else{b} } 

Now what I would like to do is not only one action in the if statement, but two, for example:

 if(a>3){a and c<<-1000} 

In this case, to return 'a', as well as writing 1000 to the variable 'c'

My question is how to add a few actions after the if statement.

+8
r
source share
1 answer

You must use a semicolon

 if(a>3){c<-1000;a} 

The last statement is the return value.

EDIT This also works for several operators. You can omit the semicolon if you use line breaks, as in

 if(a>3) { c<-1000 d<-1500 a } else { e <- 2000 b } 
+16
source share

All Articles