Here is a data.table
solution using dcast.data.table
, which takes data in the long format (your input) and converts it to the wide format required for the lm
call.
lm(`1` ~ ., dcast.data.table(dtData, Date ~ SecId, fill=0))
Here is the output of the dcast
call:
Date 1 2 3
1: 2014-01-02 -0.63 0.33 0.58
2: 2014-01-03 0.18 -0.82 -0.31
3: 2014-01-04 -0.84 0.00 0.00
4: 2014-01-05 0.00 0.49 1.51
5: 2014-01-06 1.60 0.74 0.39
I stole the lm
piece from @G.Grothendieck. Note that if you have more than three columns in your real data you will need to specify the value.var
parameter for dcast.data.table
.