0
我只是想創建一個簡單的函數,將日常收益列添加到股票價格數據框中。使用dplyr添加股票收益新列的功能
爲手動stript這看起來是這樣的:
AAL <- mutate(AAL, logreturn = Delt(AAL$Price, type = "log"))
rownames(AAL) <- AAL[, 1]
AAL <- AAL[, 2:4]
AAL[1, 3] <- 0
而且是這樣工作的。不過,我想簡單地進入
head(AAL)
Date Price Volume
1 2013-06-28 7.5839 31308
2 2013-07-01 7.2093 1054
3 2013-07-02 7.3098 109
4 2013-07-03 7.0539 1275
5 2013-07-04 7.0448 22
6 2013-07-05 7.0357 110
add_returns(AAL)
head(AAL)
Price Volume Delt.1.log
2013-06-28 7.5839 31308 0.000000000
2013-07-01 7.2093 1054 -0.050655720
2013-07-02 7.3098 109 0.013844054
2013-07-03 7.0539 1275 -0.035635258
2013-07-04 7.0448 22 -0.001290899
2013-07-05 7.0357 110 -0.001292568
我試圖做這種方式使,做上述功能,但它不工作:
add_return <- function(symbol){
symbol <- mutate(eval(parse(text = paste0(symbol, collapse = "")),
logreturn =
Delt(eval(parse(text = paste0(symbol, "$Price", collapse = ""))),
type = "log"))
rownames(symbol) <- eval(parse(text = paste0(symbol, "$Date", collapse = "")))
symbol <- eval(parse(text = paste0(symbol, "[2:4]", collapse = "")))
eval(parse(text = paste0(symbol, "[1, 3]", collapse = ""))) <- 0
}
您可能不想將日期轉換爲rownames。他們在這邊解析(例如,提取年份)會更麻煩。 – Frank