Pergunta

I have some data formatted as below. I have done some analysis on this and would like to be able to plot the price development in the same graph as the analyzed data. This requires me to have the same x-axes for the data.

So I would like to aggregate the "shares" column in say 150 increments, and add the "finalprice" and "time" to this. The aggregation should include the latest time and price, so if the aggregation needs to occur over two or more rows of data then the last row should provide the price and time data.

My question is how to create a new vector with 150 shares per row. The length of the vector will equal sum(shares)/150.

Is there an easy way to do this? Thanks in advance.

Edit: I thought about expanding the observations using rep(finalprice, shares) and then getting each 150th value of the expanded vector.

Data sample:

"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
Foi útil?

Solução

I think I got it solved.

expand <- rep(finalprice, shares) 

Increment <- expand[seq(from = 1, to = length(expand), by = 150)]
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top