Search code examples
rdataframetimegroupinglag

Find time-lag between groups in a data.frame


Let's suppose I want to estimate the time lag between two groups within a data.frame.

Here an example of my data:

df_1 = data.frame(time = c(1,3,5,6,8,11,15,16,18,20), group = 'a') # create group 'a' data
df_2 = data.frame(time = c(2,7,10,13,19,25), group = 'b') # create group 'b' data

df = rbind(df_1, df_2) # merge groups

df = df[with(df, order(time)), ] # order by time
rownames(df) = NULL #remove row names

> df
   time group
1     1     a
2     2     b
3     3     a
4     5     a
5     6     a
6     7     b
7     8     a
8    10     b
9    11     a
10   13     b
11   15     a
12   16     a
13   18     a
14   19     b
15   20     a
16   25     b

Now I need to subtract the time observation from group b to the time observation from group a. i.e. 2-1, 7-6, 10-8, 13-11, 19-18 and 25-20.

# Expected output
> out
[1] 1 1 2 2 1 5

How can I achieve this?


Solution

  • We can find indices of b and subtract the time value from it's previous index.

    inds <- which(df$group == "b")
    df$time[inds] - df$time[inds - 1]
    #[1] 1 1 2 2 1 5