R data.table返回计算和设置() [英] R data.table Return calculation and set()

查看:165
本文介绍了R data.table返回计算和设置()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我相信我缺少一些简单的东西,但是如何使用data.tables在R中计算每日回报?



假设我有一个data.table :



DT< - data.table(ticker = rep(letters,each = 5),priceA = runif(5 * 26 ^ 2 ),priceB = runif(5 * 26 ^ 2))



如何形成一个新的列,股票代码?



回报率是指正常回报率。也就是说,代码a的priceA的第二个值减去同一个代码的先前的值,并除以上一个。



给出了列ticker的例子, priceA和priceB我应该得到如下列的返回A:

  ticker priceA priceB returnsA 
1:a 0.63519775 0.04784728
2:a 0.01530738 0.34917328 -0.97590
3:a 0.28601406 0.12307475 17.68472
4:a 0.77851212 0.47829863 1.721937
5:a 0.84078779 0.23491432 0.079993

此外,如何使用 set()而不是: = 使sth像



DT [,newprice:= priceA * priceB]





谢谢! :)

解决方案

好的,这个工程:

  set.seed(42)
DT < - data.table(
ticker = rep(letters,each = 5),
priceA = runif ^ 2),
priceB = runif(5 * 26 ^ 2))

DT [,粘贴('返回',LETTERS [1:2],sep = {
lapply(.SD,function(x){
old< - head(x,-1)
new< - tail(x,-1)
c NA,(new-old)/ old)
})
} by = ticker,.SDcols = grep('^ price',names(DT))]

结果(对于此种子值)为

 股票价格A价格B回报A返回B 
1:a 0.9148060 0.7956245 NA NA
2:a 0.9370754 0.9314941 0.02434327 0.1707710
3:a 0.2861395 0.6269996 -0.69464620 -0.3268883
4: a 0.8304476 0.1666758 1.90224707 -0.7341691
5:a 0.6417455 0.6483800 -0.22722939 2.8900659
---
3376:z 0.2887293 0.3473923 -0.54132570 -0.3514041
3377:z 0.9013438 0.1788842 2.12176058 -0.4850656
3378:z 0.3126429 0.7648157 -0.65313686 3.2754788
3379:z 0.8791381 0.1300418 1.81195584 -0.8299698
3380:z 0.8160158 0.8159330 -0.07180019 5.2743905

在您的示例中使用设置

  DT [,newprice:= NA] 
set(DT,j = ncol(DT),value = DT [['priceA']] ])

此外,还有用于处理退货等的包,如下所示:使用By()计算百分比变化


I am sure I am missing something simple, but how do I calculate daily returns using data.tables in R?

Let's say I have a data.table like:

DT <- data.table(ticker=rep(letters,each=5), priceA=runif(5*26^2), priceB=runif(5*26^2))

How to I form a new column with the respective returns of price for each ticker?

By returns I mean the normal percentage returns. That is, the second value of priceA for ticker a minus the previous one for the same ticker and this divided by the previous one.

Given the example with the columns ticker, priceA and priceB I should get the column returnsA as in:

      ticker     priceA     priceB  returnsA
   1:      a 0.63519775 0.04784728 
   2:      a 0.01530738 0.34917328  -0.97590
   3:      a 0.28601406 0.12307475  17.68472 
   4:      a 0.77851212 0.47829863  1.721937
   5:      a 0.84078779 0.23491432  0.079993

Also, how do I use set() instead of := to make sth like

DT[, newprice := priceA * priceB]

?

Thank you! :)

解决方案

Okay, this works:

set.seed(42)
DT <- data.table(
  ticker=rep(letters,each=5),
  priceA=runif(5*26^2),
  priceB=runif(5*26^2))

DT[,paste('returns',LETTERS[1:2],sep=''):={
  lapply(.SD,function(x){
    old <- head(x,-1)
    new <- tail(x,-1)
    c(NA,(new-old)/old)
  })
},by=ticker,.SDcols=grep('^price',names(DT))]

The result (for this seed value) is

      ticker    priceA    priceB    returnsA   returnsB
   1:      a 0.9148060 0.7956245          NA         NA
   2:      a 0.9370754 0.9314941  0.02434327  0.1707710
   3:      a 0.2861395 0.6269996 -0.69464620 -0.3268883
   4:      a 0.8304476 0.1666758  1.90224707 -0.7341691
   5:      a 0.6417455 0.6483800 -0.22722939  2.8900659
  ---                                                  
3376:      z 0.2887293 0.3473923 -0.54132570 -0.3514041
3377:      z 0.9013438 0.1788842  2.12176058 -0.4850656
3378:      z 0.3126429 0.7648157 -0.65313686  3.2754788
3379:      z 0.8791381 0.1300418  1.81195584 -0.8299698
3380:      z 0.8160158 0.8159330 -0.07180019  5.2743905

To use set in your example:

DT[,newprice:=NA]
set(DT,j=ncol(DT),value=DT[['priceA']]*DT[['priceB']])

Also, there are packages designed for dealing with returns and such, as seen here: Calculating %changes with the By()

这篇关于R data.table返回计算和设置()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆