作者laputaca (离歌笑)
看板R_Language
标题[问题] 关於switch选择
时间Mon Nov 16 22:55:51 2015
[问题叙述]:
在function里面使用switch时
[程式范例]:
比如说我想要
当fruit=apple
x=x[,2]
y=y[,2]
fruit=grape
x=x[,3]
y=y[,3]
则写成程式
data = function(x, fruit)
{
switch(fruit, apple=????
grape=????这个地方完全就不知道怎麽打... )
}
if else又只能用在数字...
还是说除了switch之外,有其他[名词]条件选择的指令呢?
感谢各位大大
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 114.33.105.103
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/R_Language/M.1447685755.A.212.html
1F:→ celestialgod: 什麽是if else只能用在数字? 11/16 22:56
2F:→ celestialgod: if (fruit == 'apple') 还是可以做得到阿 11/16 22:56
3F:→ celestialgod: col = switch(a, apple = 2, grape = 3) 11/16 22:57
4F:→ celestialgod: 再拿col做事就好了 11/16 22:57
5F:→ laputaca: 原来是我误会了XDDDD难怪我想破脑袋 感恩感恩 11/16 23:00