且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在 R 函数中使用 switch 语句?

更新时间:2023-01-19 17:38:02

嗯,switch 可能并不是真的要这样工作,但你可以:

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')
}
)

...每个 case 都是一个表达式——通常只是一个简单的事情,但在这里我使用了一个卷曲块,这样你就可以在那里填充你想要的任何代码......

...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...