If statement with multiple actions in R

前端 未结 1 1284
故里飘歌
故里飘歌 2020-12-31 07:10

I would like to write an if statement of the following form:

a=5
b=2

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

Now what I would like t

相关标签:
1条回答
  • 2020-12-31 07:42

    You should use the semicolon

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

    The last statement is the return value.

    EDIT This works for multiple statements, too. You can omit the semicolon if you use line breaks, as in

    if(a>3) {
      c<-1000
      d<-1500
      a
    } else {
      e <- 2000
      b
    }
    
    0 讨论(0)
提交回复
热议问题