R累积股票数据 - 增加时间和价格

时间:2012-12-12 11:14:30

标签: r accumulate

我有一些格式如下的数据。我已对此做了一些分析,并希望能够在与分析数据相同的图表中绘制价格发展。 这要求我对数据使用相同的x轴。

所以我想以150个增量汇总“份额”列,并为此添加“finalprice”和“time”。 聚合应包括最新的时间和价格,因此如果聚合需要在两行或更多行数据上进行,则最后一行应提供价格和时间数据。

我的问题是如何创建一个每行150个共享的新向量。 向量的长度将等于sum(shares)/ 150。

有一种简单的方法吗?提前致谢。

修改: 我考虑使用rep(finalprice,shares)扩展观察结果,然后获得扩展向量的每个第150个值。

数据样本:

"date","ord","shares","finalprice","time","stock"
20120702,E,2000,99.35,540.84753333,500
20120702,E,28000,99.35,540.84753333,500
20120702,E,50,99.5,542.03073333,500
20120702,E,13874,99.5,542.29411667,500
20120702,E,292,99.5,542.30191667,500
20120702,E,784,99.5,542.30193333,500
20120702,E,13300,99.35,543.04805,500
20120702,E,16658,99.35,543.04805,500
20120702,E,42,99.5,543.04805,500
20120702,E,400,99.4,546.17173333,500
20120702,E,100,99.4,547.07,500
20120702,E,2219,99.3,549.47988333,500
20120702,E,781,99.3,549.5238,500
20120702,E,50,99.3,553.4052,500
20120702,E,1500,99.35,559.86275,500
20120702,E,103,99.5,567.56726667,500
20120702,E,1105,99.7,573.93326667,500
20120702,E,4100,99.5,582.2657,500
20120702,E,900,99.5,582.2657,500
20120702,E,1024,99.45,582.43891667,500
20120702,E,8214,99.45,582.43891667,500
20120702,E,10762,99.45,582.43895,500
20120702,E,1250,99.6,586.86446667,500
20120702,E,5000,99.45,594.39061667,500
20120702,E,20000,99.45,594.39061667,500
20120702,E,15000,99.45,594.39061667,500
20120702,E,4000,99.45,601.34491667,500
20120702,E,8700,99.45,603.53608333,500
20120702,E,3290,99.6,609.23213333,500

1 个答案:

答案 0 :(得分:0)

我想我已经解决了。

expand <- rep(finalprice, shares) 

Increment <- expand[seq(from = 1, to = length(expand), by = 150)]