Reputation: 211
I new with R. I am trying to aggregate data in 5 minutes blocks. I would like to have the open, the close,the high,the low, and the total Size. I have added the highfrequency package, along with zoo package and xts package. I am following the tutorial (section 3.2) from the people who create the package from this link: http://highfrequency.herokuapp.com/
Here is the error I get:
Error in seq.default(start(ts), end(ts), by = tby) :
'from' must be of length 1
Here is a sample of the data I am using . The file is much bigger, but I don't know how to attach it to my post:
Date,Time,Price,Size
02/18/2014,05:06:13,49.6,200
02/18/2014,05:06:13,49.6,200
02/18/2014,05:06:13,49.6,200
02/18/2014,05:06:14,49.6,200
02/18/2014,05:06:14,49.6,193
02/18/2014,05:44:41,49.62,100
02/18/2014,06:26:36,49.52,100
02/18/2014,06:26:36,49.52,500
02/18/2014,07:09:29,49.6,100
02/18/2014,07:56:40,49.56,300
02/18/2014,07:56:40,49.55,400
02/18/2014,07:56:41,49.54,200
02/18/2014,07:56:43,49.55,100
02/18/2014,07:56:43,49.55,100
02/18/2014,07:56:50,49.55,100
02/18/2014,07:57:12,49.53,100
02/18/2014,07:57:12,49.51,2200
02/18/2014,07:57:12,49.51,100
02/18/2014,07:57:12,49.5,200
here is my code:
library(highfrequency)
data1<-read.table("C_0.txt",sep=",",header=T,stringsAsFactors=F)
ts=data1$Price
tsagg5min=aggregatets(ts,on="minutes",k=5)
Do you have a suggestion on how I can achieve my aim with very large data set? I run a windows 8 64 bit machine.
Thank you for you help.
Upvotes: 0
Views: 689
Reputation: 99351
You can use split
, cut
, and strptime
to cut the data into 5 minute intervals. Assuming dat
is your data,
> x <- split(dat, cut(strptime(dat$Time, format="%R"),"5 mins"))
> x
$`2014-02-27 05:06:00`
Date Time Price Size
1 02/18/2014 05:06:13 49.6 200
2 02/18/2014 05:06:13 49.6 200
3 02/18/2014 05:06:13 49.6 200
4 02/18/2014 05:06:14 49.6 200
5 02/18/2014 05:06:14 49.6 193
...
...
...
$`2014-02-27 07:56:00`
Date Time Price Size
10 02/18/2014 07:56:40 49.56 300
11 02/18/2014 07:56:40 49.55 400
12 02/18/2014 07:56:41 49.54 200
13 02/18/2014 07:56:43 49.55 100
14 02/18/2014 07:56:43 49.55 100
15 02/18/2014 07:56:50 49.55 100
16 02/18/2014 07:57:12 49.53 100
17 02/18/2014 07:57:12 49.51 2200
18 02/18/2014 07:57:12 49.51 100
19 02/18/2014 07:57:12 49.50 200
Remove the empty time blocks,
x <- x[sapply(x, dim)[1,] != 0]
Get the high Price
> sapply(x, function(y) max(y$Price))
2014-02-27 05:06:00 2014-02-27 05:41:00 2014-02-27 06:26:00 2014-02-27 07:06:00
49.60 49.62 49.52 49.60
2014-02-27 07:56:00
49.56
Get the low Price
> sapply(x, function(y) min(y$Price))
2014-02-27 05:06:00 2014-02-27 05:41:00 2014-02-27 06:26:00 2014-02-27 07:06:00
49.60 49.62 49.52 49.60
2014-02-27 07:56:00
49.50
Get the total Size
> sapply(x, function(y) sum(y$Size))
2014-02-27 05:06:00 2014-02-27 05:41:00 2014-02-27 06:26:00 2014-02-27 07:06:00
993 100 600 100
2014-02-27 07:56:00
3800
Upvotes: 1