添加一个返回到R中具有不同股票的数据框的列

时间:2016-03-06 21:47:04

标签: r dplyr

我有一个包含不同股票每日价格的数据集。我的数据结构如下:id表示不同的股票(例如,Apple Inc,General Electric,..):

data_set
id      date        price
66      2012-12-13  99.50725
66      2012-12-14  99.95988
66      2012-12-15  99.92672
66      2012-12-16  99.99344
66      2012-12-17  99.82329
66      2012-12-18  101.01595
66      2012-12-19  100.53868
66      2012-12-20  102.29878
66      2012-12-21  100.71146
66      2012-12-27  98.69724
...
2407    2012-11-22  99.97662
2407    2012-11-23  100.13158
2407    2012-11-24  100.27341
2407    2012-11-25  100.27769
2407    2012-11-26  99.90592
2407    2012-11-27  100.18082
2407    2012-11-28  100.46661
2407    2012-11-29  100.7861
2407    2012-11-30  100.4614
...
10247   2013-10-09  99.03381
10247   2013-10-10  99.36548
10247   2013-10-14  99.46137
10247   2013-10-15  100.09372
10247   2013-10-16  100.13352
10247   2013-10-17  100.15828
10247   2013-10-18  100.83093
10247   2013-10-19  101.29091
10247   2013-10-20  101.3583
...

现在我想添加一个额外的列,其中包含每个股票的每日回报。我使用dplyr尝试了以下内容:

calc_return <- function(returns_vector) {log(returns_vector[-1]/returns_vector[-length(returns_vector)])}

returns <- data_set %>%
           group_by(id) %>%
           summarize(ret=calc_return(price))  

不幸的是,此解决方案无法运行。我会很感激任何提示。

谢谢!

1 个答案:

答案 0 :(得分:2)

尝试以下方法:

library(dplyr)
data_set %>% group_by(id) %>% mutate(ret=log(price/lag(price)))
Source: local data frame [28 x 4]
Groups: id [3]

      id       date     price           ret
   (int)     (fctr)     (dbl)         (dbl)
1     66 2012-12-13  99.50725            NA
2     66 2012-12-14  99.95988  0.0045383997
3     66 2012-12-15  99.92672 -0.0003317881
4     66 2012-12-16  99.99344  0.0006674665
5     66 2012-12-17  99.82329 -0.0017030610
6     66 2012-12-18 101.01595  0.0118769023
7     66 2012-12-19 100.53868 -0.0047358961
8     66 2012-12-20 102.29878  0.0173552181
9     66 2012-12-21 100.71146 -0.0156381506
10    66 2012-12-27  98.69724 -0.0202026141