看板 R_Language 關於我們 聯絡資訊
※ [本文轉錄自 Statistics 看板 #1DSTy6Wc ] 作者: gsuper (統計的巴比倫塔) 看板: Statistics 標題: Re: [R程式] 寫出含文字的函數? 時間: Sat Mar 5 14:58:11 2011 程式 ============================================================ f <- function(input) { formula <- "log2(input)+1/15" if(is.character(input)) { gsub("input",input,formula) } else if(is.numeric(input)) { eval(parse(text=formula)) } else { return("資料格式不正確") } } ####### 結果 ####### a <- 1 b <- "x" c <- "y" d <- as.factor(10) SEE <- list(a=f(a),b=f(b),c=f(c),d=f(d)) -- -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.113.239.247
diplazium:g大好強~大推! 03/05 17:11
nago:謝謝g大!! 我對變數宣告真的不是很熟~~我會參考12483那篇的 03/05 23:56
nago:再次由衷感謝!!~~~XD 03/05 23:56
gsuper:eval 那段真的很強 , 不過是從 click 大那邊抄過來的 03/06 05:17
clickhere:gsub在這邊可能會有問題.除非"input"字串唯一. 03/06 14:17
clickhere:help("substitute") and help("expression") 03/06 14:18
clickhere:應可以省略 is.character and is.numeric 03/06 14:19
clickhere:substitute(expression(a + b), list(a = "AA")) 03/06 14:21
clickhere:substitute(a + b, list(a = "AA")) 03/06 14:21
clickhere:click也是抄來的. 03/06 14:23
clickhere:比如說function name不叫log2(), 而是input()... 03/06 14:28
※ 發信站: 批踢踢實業坊(ptt.cc) ※ 轉錄者: gsuper (140.113.239.247), 時間: 03/30/2013 20:35:25