How to group Time Series data into round intervals of 5 minutes in R? -
i have time series data frame looking this:
time source value 1 2016-01-20 15:10:04 c04 open 2 2016-01-20 15:09:57 m04 true 3 2016-01-20 15:09:53 m02 true 4 2016-01-20 15:09:53 m03 true 5 2016-01-20 14:44:54 m04 true
now group them in intervals of 5 minutes starting 00:00:00, intervals of 0-5-10-15-20... , on. intervals shall used later group identifier:
time source value group 1 2016-01-20 15:10:04 c04 open 10 2 2016-01-20 15:09:57 m04 true 5 3 2016-01-20 15:09:53 m02 true 5 4 2016-01-20 15:09:53 m03 true 5 5 2016-01-20 14:44:54 m04 true 40
i tried cut() dates using breaks="5 min" instead of getting round start , end values, result looks this:
> table(cut.posixt(df.formatted$time, breaks="5 min"))[1:5] 2015-12-31 12:49:00 2015-12-31 12:54:00 2015-12-31 12:59:00 2015-12-31 13:04:00 2015-12-31 13:09:00 4 0 0 1 15
is there way tell cut() use round time intervals? tried grouping using xts package, got more confused ohlc helped me. tried using her.misc package (see time.factor documentation due poor documentation couldn't run properly.
does maybe know how solve problem?
first, need install "chron" package. package has minutes()
function gives minute of time.
i have paste first , second column don't think have too. use tmptime <- tmp[,1]
library(chron) tmp <- read.table(text="time source value 2016-01-20 15:10:04 c04 open 2016-01-20 15:09:57 m04 true 2016-01-20 15:09:53 m02 true 2016-01-20 15:09:53 m03 true 2016-01-20 14:44:54 m04 true", header=t, row.names= null) tmptime <- paste(tmp[,1], tmp[,2]) group <- seq(0,55,5) sapply(tmptime, function(x){ x <- minutes(x) for(i in 2:length(group)){ if(x < group[i]) {return(group[i-1]); break} else if(x >= group[length(group)]) return(group[length(group)]) } }) [1] 10 5 5 5 40
Comments
Post a Comment