Here's a solution for you. I leveraged data.table
because it has some nice time functionality and is highly performant at group-by calculations. I created a time index, collapsing based on year, year day, and binning by hour. You would need to modify this if you intend to group by the "last hour" of R programming based off system time. In that case, Sys.time()
can be your friend.
Anyhow, here's the solution:
df <- data.frame(c("Attribute1", "Attribute1", "Attribute1", "Attribute2", "Attribute2"),
c("2018-11-01 00:00:19", "2018-11-01 00:00:54", "2018-11-01 00:01:17",
"2018-11-01 00:01:23", "2018-11-01 00:01:25"))
names(df) <- c("Signature", "date")
df$date <- as.POSIXct(df$date)
library(data.table)
dt <- setDT(df)
dt[, time_idx := paste0(year(date), "-", yday(date), "-", hour(date))]
dt[, Count_Signature := (1L:.N) - 1L, keyby = .(Signature, time_idx)]
dt
#> Signature date time_idx Count_Signature
#> 1: Attribute1 2018-11-01 00:00:19 2018-305-0 0
#> 2: Attribute1 2018-11-01 00:00:54 2018-305-0 1
#> 3: Attribute1 2018-11-01 00:01:17 2018-305-0 2
#> 4: Attribute2 2018-11-01 00:01:23 2018-305-0 0
#> 5: Attribute2 2018-11-01 00:01:25 2018-305-0 1
Created on 2019-01-03 by the reprex package (v0.2.1)