Res1234
Res1234

Reputation: 53

Include trend variable from certain time period R

I have been searching a while, but I don't find how to do the following in R:

I have 600 panel datasets containing a variable identifying the cross-sections (group), the time (time), and a dummy variable (dummy) that takes the value of 1 from a certain time period onwards. This time period varies across the multiple files that I would have. In other words, across all my files, time from which period on dummy takes the value of 1 is always different (can be 36,37,38,320,...). The time from which dummy takes 1 is always the same across the groups within each file.

I would need to add another variable (trend) that is a new trend variable that equals 1 from the moment in time (time) that dummy equals 1 for the first time.

group   time    dummy   trend
1        36      0       0
1        37      1       1
1        38      1       2
2        36      0       0
2        37      1       1
2        38      1       2

Upvotes: 0

Views: 747

Answers (1)

Jason
Jason

Reputation: 1569

Here is an answer with data.table

library(data.table)
test_dt<-data.table(group=c(rep(1,3),rep(2,3)),time=c(36:38,36:38),dummy=c(0,1,1,0,1,1))

test_dt[,trend:=cumsum(dummy),by=group] #your operation

 group time dummy trend
1:     1   36     0     0
2:     1   37     1     1
3:     1   38     1     2
4:     2   36     0     0
5:     2   37     1     1
6:     2   38     1     2

This relies on your dummy being 0/1, the data being sorted and that the dummy is always 1 after the first time it turns 1 within that group.

Upvotes: 1

Related Questions