[R-SIG-Finance] Test for new event and save data in new data.frame
Mark Knecht
markknecht at gmail.com
Thu Oct 4 20:11:03 CEST 2012
On Thu, Oct 4, 2012 at 7:49 AM, R. Michael Weylandt
<michael.weylandt at gmail.com> wrote:
> On Thu, Oct 4, 2012 at 12:12 AM, Mark Knecht <markknecht at gmail.com> wrote:
<SNIP>
>> TradeData = data.frame(cbind(t1,t2,t3,t4))
>
> This is a terrible awful and altogether wretched idiom. Instead just
> do data.frame(t1, t2, t3, t4). cbind()-ing completely invalidates the
> entire point of a data.frame() by coercing all its inputs to a common
> class. [Don't take it personally: we've just been working to stamp it
> out every where we see it on the main R-help list for a few months
> now]
>
Hi Michael,
Not in the least insulted or anything like that. I'm just trying to
learn. Thanks for alluding to reasons it's not preferred. I'll try to
keep that in mind.
>
> Best advice: don't use a data.frame() for this sort of data. Instead,
> use a proper time series object, like xts or zoo. These also provide a
> lag function for you:
>
> library(xts)
>
> PriceData <- xts(cbind(BarNum = 1:20, Price =
> c(5,6,7,8,1,2,3,4,7,2,3,4,5,7,8,9,1,2,1,1), MP =
> c(0,0,1,1,1,0,0,1,0,0,1,1,0,0,1,1,1,0,0,0)), Sys.Date() + 1:20)
>
> print(PriceData)
>
> # Now we can add on the lag column:
>
> PriceData <- cbind(PriceData, LagMP = lag(PriceData[,3], 3))
>
> with(PriceData, (MP == 1) & (MP.1== 0)) # Gives you the rows you want
> to manipulate
>
> Cheers,
> Michael
OK, that all works pretty well including the with(PriceData, ... part.
Thanks. One question though - Is the 'LagMP' inside the cbind supposed
to give me the name LagMP in the result? The columns are named MP.1 &
MP.2 so I renamed it explicitly like this:
colnames(PriceData) = c(colnames(PriceData[,1:3]),"LagMP","LongEntry")
Maybe there's a cbind option to get the name in but I couldn't find it.
> library(xts)
>
> PriceData <- xts(cbind(BarNum = 1:20,
+ Price = c(5,6,7,8,1,2,3,4,7,2,3,4,5,7,8,9,1,2,1,1),
+ MP = c(0,0,1,1,1,0,0,1,0,0,1,1,0,0,1,1,1,0,0,0)),
+ Sys.Date() + 1:20)
>
> # Now we can add on the lag column:
> PriceData <- cbind(PriceData, LagMP = lag(PriceData[,3], 1))
> PriceData <- cbind(PriceData, LongEntry = with(PriceData,(MP == 1) & (MP.1 == 0)))
>
> print(PriceData[1:5,])
BarNum Price MP MP.1 MP.2
2012-10-05 1 5 0 NA 0
2012-10-06 2 6 0 0 0
2012-10-07 3 7 1 0 1
2012-10-08 4 8 1 1 0
2012-10-09 5 1 1 1 0
>
> colnames(PriceData) = c(colnames(PriceData[,1:3]),"LagMP","LongEntry")
>
> print(PriceData[1:5,])
BarNum Price MP LagMP LongEntry
2012-10-05 1 5 0 NA 0
2012-10-06 2 6 0 0 0
2012-10-07 3 7 1 0 1
2012-10-08 4 8 1 1 0
2012-10-09 5 1 1 1 0
Next step is, I think, to use the LongEntry value to create a new
data.frame, or maybe a matrix instead, to hold the actual trades as
they progress. I think that's done with some form of apply? Reading my
'Data Manipulation in R' book but haven't found an example of anything
similar yet.
Thanks,
Mark
More information about the R-SIG-Finance
mailing list