2

I've been looking at this use of cut function: With R: Split time series data into time intervals (say an hour) and then plot the count

I was wondering if there was a way to perform this with just the time. For example:

2001/01/10 9:54
2002/01/10 9:55
2004/01/10 9:50

I would like it to count 3 under the 9:00-9:59 count. Such as:

9:00-59 - count 3

Instead of three different 9:00-59 for each day like:

2001/01/10 9:00-59 - count 1
2002/01/10 9:00-59 - count 1
2004/01/10 9:00-59 - count 1

Hope this makes sense for everyone.

Community
  • 1
  • 1
JKay96
  • 73
  • 6

1 Answers1

2

A simple way of solving this using base R is to reformat the date time column to just hours such as this:

sampletimes<-as.POSIXct(c("2001/01/10 9:54",  "2002/01/10 9:55", "2004/01/10 9:50"))

output<-tapply(sampletimes, format(sampletimes, "%H"), length)

The names(output) will list just the integer values of the hours so you may need some edits depending on the final presentation needs.

Dave2e
  • 22,192
  • 18
  • 42
  • 50
  • This doesn't really help me in the problem I had. – JKay96 Mar 18 '17 at 17:14
  • @jkay96, Can you edit your question in order to make it clearer? – Dave2e Mar 18 '17 at 17:49
  • 1
    @JKay96 ; Can you add details to your question to show why this does this not solve the problem? it assigns the hours, independent of date, to the same group, which seems is what you want? (ie just the `format(sampletimes, "%H")` part). Dave2e seems `table(format(sampletimes, "%H"))` would do for getting the counts rather thanl oop. – user20650 Mar 18 '17 at 19:05