作者gsuper (Logit(odds))
看板R_Language
標題Fw: [R程式] 寫出含文字的函數?
時間Sat Mar 30 20:35:25 2013
※ [本文轉錄自 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
1F:推 diplazium:g大好強~大推! 03/05 17:11
2F:推 nago:謝謝g大!! 我對變數宣告真的不是很熟~~我會參考12483那篇的 03/05 23:56
3F:→ nago:再次由衷感謝!!~~~XD 03/05 23:56
4F:→ gsuper:eval 那段真的很強 , 不過是從 click 大那邊抄過來的 03/06 05:17
5F:→ clickhere:gsub在這邊可能會有問題.除非"input"字串唯一. 03/06 14:17
6F:→ clickhere:help("substitute") and help("expression") 03/06 14:18
7F:→ clickhere:應可以省略 is.character and is.numeric 03/06 14:19
8F:→ clickhere:substitute(expression(a + b), list(a = "AA")) 03/06 14:21
9F:→ clickhere:substitute(a + b, list(a = "AA")) 03/06 14:21
10F:→ clickhere:click也是抄來的. 03/06 14:23
11F:→ clickhere:比如說function name不叫log2(), 而是input()... 03/06 14:28
※ 發信站: 批踢踢實業坊(ptt.cc)
※ 轉錄者: gsuper (140.113.239.247), 時間: 03/30/2013 20:35:25