[R] time bin sum

Pete Brecknock Peter.Brecknock at bp.com
Wed Feb 2 05:37:56 CET 2011


Jessica

Any good?

lines <-"DateTime, Q
2004-12-09 15:30:01, 2
2004-12-09 15:30:01, 1
2004-12-09 15:30:06, 1
2004-12-09 15:30:14, 5
2004-12-09 15:30:21, 1
2004-12-09 15:30:22, 11
2004-12-09 15:30:24, 4
2004-12-09 15:30:32, 1
2004-12-09 15:30:32, 1
2004-12-09 15:30:32, 3
2004-12-09 15:30:41, 4"

d = read.table(textConnection(lines), sep="," ,header = TRUE)

d$DateTime = as.POSIXct(d$DateTime) 

time <- seq(as.POSIXct('2004-12-09 15:30:00'),by='5 sec', length=10)

bins = cut(d$DateTime,breaks=time)

counts = as.data.frame(tapply(d$Q,bins,sum))

# Clean up 
counts[is.na(counts)]=0
colnames(counts) = "Counts"

print(counts)

HTH

Pete
-- 
View this message in context: http://r.789695.n4.nabble.com/time-bin-sum-tp3252376p3253400.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list