Hi I have a intra day data table with the following columns :
date, stock_id, timestamp, price
First I added keys to order this properly :
setkeyv( my_table, c('stock_id','date','timestamp'))
The data looks like :
date timestamp stock_id price
2011-01-04 1.294128e+12 7 3402.0
2011-01-04 1.294129e+12 7 3402.5
2011-01-04 1.294129e+12 7 3407.5
Now I would like to convert the stock_price to returns and log returns.
Could you please point to an efficient/elegant way to do this in R and data.table grouping without resorting to loops?
Many thanks in advance, I’m very new to R.
As it happens, you’ve asked something that is most easily done in the latest version (v1.8.1) on R-Forge repo using a new feature:
:=by group.How about (untested) :
To install v1.8.1 from R-Forge without compiling from source, you need to be running R 2.15.0 or later and then just type :