作者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/m.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