Guy
Guy

Reputation: 11

How to bin observations over a time series in r?

I have an data set which looks like this:

  VisitID                  Start
1       0 2015-02-15 09:46:43.17
2       1 2015-02-15 09:47:37.84
3       2 2015-02-15 09:58:46.42
4       3 2015-02-15 09:58:48.46
5       4 2015-02-15 10:28:25.09
6       5 2015-02-15 10:33:43.53

I want to make a bar plot of count per one hour(y-axis) vs. absolute time(x-axis), meaning how many observations were in one hour. can you please help? Thanks, Guy

Upvotes: 0

Views: 73

Answers (1)

digEmAll
digEmAll

Reputation: 57210

Something like this should work :

DF <- read.csv(text=
"VisitID,Start
0,2015-02-15 09:46:43.17
1,2015-02-15 09:47:37.84
2,2015-02-15 09:58:46.42
3,2015-02-15 09:58:48.46
4,2015-02-15 10:28:25.09
5,2015-02-15 10:33:43.53",stringsAsFactors=FALSE)

DF$StartDate <- strptime(DF$Start, tz='GMT', format="%Y-%m-%d %H:%M:%OS")

hours <- vapply(split(1:nrow(DF),format(DF$StartDate,"%Y-%m-%d %H:00:00",tz='UTC')),length,0)

barplot(hours)

enter image description here

Upvotes: 2

Related Questions