[問題] 關於switch選擇

作者: laputaca (離歌笑)   2015-11-16 22:55:51
[問題敘述]:
在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之外,有其他[名詞]條件選擇的指令呢?
感謝各位大大
作者: celestialgod (天)   2015-11-16 22:56:00
什麼是if else只能用在數字?if (fruit == 'apple') 還是可以做得到阿col = switch(a, apple = 2, grape = 3)再拿col做事就好了
作者: laputaca (離歌笑)   2015-11-16 23:00:00
原來是我誤會了XDDDD難怪我想破腦袋 感恩感恩

Links booklink

Contact Us: admin [ a t ] ucptt.com