How to use the switch statement in R functions?

前端 未结 4 1038
傲寒
傲寒 2020-11-29 19:32

I would like to use for my function in R the statement switch() to trigger different computation according to the value of the function\'s argument.

Fo

4条回答
  •  眼角桃花
    2020-11-29 20:11

    Well, switch probably wasn't really meant to work like this, but you can:

    AA = 'foo'
    switch(AA, 
    foo={
      # case 'foo' here...
      print('foo')
    },
    bar={
      # case 'bar' here...
      print('bar')    
    },
    {
       print('default')
    }
    )
    

    ...each case is an expression - usually just a simple thing, but here I use a curly-block so that you can stuff whatever code you want in there...

提交回复
热议问题