Home > front end >  R - Create Event Time variable
R - Create Event Time variable

Time:11-13

I am using panel data that looks like this.

d <- data.frame(id = c("a", "a", "a", "a", "a", "b", "b", "b", "b", "b", "c", "c", "c", "c", "c"),
                time = c(1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5),
                iz = c(0,1, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1))
   id time iz
1   a    1  0
2   a    2  1
3   a    3  1
4   a    4  0
5   a    5  0
6   b    1  0
7   b    2  0
8   b    3  0
9   b    4  0
10  b    5  1
11  c    1  0
12  c    2  0
13  c    3  0
14  c    4  1
15  c    5  1

Now I want to create an event time indicator that measures the time since the first event as below.

  id time iz nvar
1   a    1  0   -1
2   a    2  1    0
3   a    3  1    1
4   a    4  1    2
5   a    5  1    3
6   b    1  0   -4
7   b    2  0   -3
8   b    3  0   -2
9   b    4  0   -1
10  b    5  1    0
11  c    1  0   -1
12  c    2  0   -2
13  c    3  0   -3
14  c    4  1    0
15  c    5  1    1

I have tried the solutions in the link posted below but can't make it work in my case. Especially I struggle to make it to count the time after event as specified as above. Let me know in case you have any advice on what I should try.

Create a time to and time after event variables

CodePudding user response:

We could subtract row sequence (row_number() or the time column) from the first matching index for each 'id' (grouped by id)

library(dplyr)
d <- d %>% 
   group_by(id) %>%
   mutate(nvar = row_number() - match(1, iz)) %>%
   ungroup

-output

d
# A tibble: 15 × 4
   id     time    iz  nvar
   <chr> <dbl> <dbl> <int>
 1 a         1     0    -1
 2 a         2     1     0
 3 a         3     1     1
 4 a         4     1     2
 5 a         5     1     3
 6 b         1     0    -4
 7 b         2     0    -3
 8 b         3     0    -2
 9 b         4     0    -1
10 b         5     1     0
11 c         1     0    -3
12 c         2     0    -2
13 c         3     0    -1
14 c         4     1     0
15 c         5     1     1

CodePudding user response:

After grouping by id, you can try subtracting time for a given row by time where the first iz is equal to 1.

library(dplyr)

d %>%
  group_by(id) %>%
  mutate(nvar = time - first(time[iz == 1]))

Output

   id     time    iz  nvar
   <chr> <dbl> <dbl> <dbl>
 1 a         1     0    -1
 2 a         2     1     0
 3 a         3     1     1
 4 a         4     1     2
 5 a         5     1     3
 6 b         1     0    -4
 7 b         2     0    -3
 8 b         3     0    -2
 9 b         4     0    -1
10 b         5     1     0
11 c         1     0    -3
12 c         2     0    -2
13 c         3     0    -1
14 c         4     1     0
15 c         5     1     1
  • Related