I am looking to calculate a new column "congestion" by counting the number of times values are within sec +/- 5 and within x +/- 5 and within y +/- 5 of the current row. Essentially I am wanting to find observations that occur within a close distance (x,y) and time period (sec) of the current observation which is just a big count ifelse statement. All values are numerical.
current data.table
data <- data.table(x = c(1,3,10,15,6),
y = c(5,5,11,14,19),
sec=c(1,3,5,6,9))
desired output
data <- data.table(x = c(1,3,10,15,6),
y = c(5,5,11,14,6),
sec=c(1,3,5,6,7),
congestion = c(1,2,1,1,2)
preferable solution in data.table but happy to work in dplyr.