Skip to content

time bin sum

1 message · Pete Brecknock

#
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