Hi all,

I have a large dataset in which each observation contains a time stamp. Now I'm trying to calculate a new variable x that counts, for each observation, how many other observations happened within +/- 5 minute of that observation. I tried doing this with a for-loop (see below), but for some reason the loop uses 0.7 s per cycle, which makes it way to slow for the amount of observations I have. My hunch is that there is an easy solution to this using lapply, but I have no experience using the apply family of function. Thanks in advance for any help!

Klemens

for (i in 1 : nrow(data1)) {

data1$x <- nrow(subset(data1, difftime(data1$DateTime

I have a large dataset in which each observation contains a time stamp. Now I'm trying to calculate a new variable x that counts, for each observation, how many other observations happened within +/- 5 minute of that observation. I tried doing this with a for-loop (see below), but for some reason the loop uses 0.7 s per cycle, which makes it way to slow for the amount of observations I have. My hunch is that there is an easy solution to this using lapply, but I have no experience using the apply family of function. Thanks in advance for any help!

Klemens

for (i in 1 : nrow(data1)) {

data1$x <- nrow(subset(data1, difftime(data1$DateTime

*, data1$DateTime, unit = "min") < 5*

& difftime(data1$DateTime& difftime(data1$DateTime

*, data1$DateTime, unit = "min") > -5))*

}}

Last edited: