I have a question about the extraction of multiple values from a data.frame in R and putting them into a new data.frame.
I have a data.frame that looks like this (df)
PRICE EVENT
1.50 0
1.70 0
1.65 0
1.20 1
0.90 0
1.70 0
1.55 0
. .
. .
1.10 0
1.20 0
1.14 1
0.90 0
My actual data.frame has these two columns and over 300.000 rows. The column called EVENT only has the values 0 OR 1 (the value 1 is a proxy that a certain event occurs).
First Step of my research: Analyze the price if the Event occurs. The first step is a easy one. I did it with
vector<-df[df$EVENT==1, "PRICE"]
now vector
contains all the Prices for the Eventdays. (here: 1.20 and 1.14)
but now the second step of my research is where it gets interesting:
now i want not only the prices for the eventday, but also the prices for x days before and after the eventday and put them into a matrix
For Example: I want the prices of two days before the event and one day after the event (including event day)
than the new data.frame i am trying to create would look like
Event 1 Event n
-2 1.70 ... 1.10
-1 1.65 ... 1.20
0 1.20 ... 1.14
+1 0.90 ... 0.90
Please keep in mind that the 4 days span [-2:1] is only an example. In my actual research i have to cover a 91 day span [-30:60].
Thanks for the help :)