簡體   English   中英

在R中具有不同庫存的數據框中添加帶有收益的列

[英]Add a column with returns to a data frame with different stocks in R

我有一個包含不同股票每日價格的數據集。 我的數據結構如下例,其中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以下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))  

不幸的是,該解決方案無法運行。 我將不勝感激。

謝謝!

請嘗試以下操作:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM