看板 R_Language 關於我們 聯絡資訊
[問題敘述]: 在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://www.ptt.cc/bbs/R_Language/M.1447685755.A.212.html
celestialgod: 什麼是if else只能用在數字? 11/16 22:56
celestialgod: if (fruit == 'apple') 還是可以做得到阿 11/16 22:56
celestialgod: col = switch(a, apple = 2, grape = 3) 11/16 22:57
celestialgod: 再拿col做事就好了 11/16 22:57
laputaca: 原來是我誤會了XDDDD難怪我想破腦袋 感恩感恩 11/16 23:00