# Creating a conditional lag variable in R

7 messages
Open this post in threaded view
|

## Creating a conditional lag variable in R

 Dear R-users, I’ve a rather complicated task to do and need all the help I can get. I have data indicating whether a country has signed an agreement or not (1=yes and 0=otherwise). I want to simply create variable that would capture the years before the agreement is signed. The aim is to see whether pre or post agreement period has any impact on my dependent variables. More preciesly, I want to create the following variables: (i) a variable that is =1 in the 4 years pre/before the agreement, 0 otherwise; (ii) a variable that is =1 5 years pre the agreement and (iii) a variable that would count the 4 and 5 years pre the agreement (1,2,3,4..). Please see the sample data below. I have manually added the variables I would like to generate in R, labelled as “X1_pre4” ( 4 years before the agreement X1), “X2_pre4”, “X1_pret5” ( 5 years before the agreement X5), and “X1pre5_count” (which basically count the years, 1,2,3, etc). The X1 and X2 is the agreement that countries have either signed (1) or not (0). Note though that I want the variable to capture all the years up to 4 and 5. If it’s only 2 years, it should still be ==1 (please see the example below). To illustrate the logic: the country A has signed the agreement X1 in 1972 in the sample data,  then, the (i) and (ii) variables as above should be =1 for the years 1970, 1971, and =0 from 1972 until the end of the study period. The country A has signed the agreement X2 in 1975,  then, the (i) variable should be =1 from 1971 to 1974 (post 4 years) and (ii) should be =1 for the  1970-1974  period (post 5 years before the agreement is signed). Later, I would also like to create post_4 and post_5 variables, but I think I’ll be able to figure it out once I know how to generate the pre/before variables. All suggestions are much appreciated! data<–structure(list(country = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("A", "B", "C"), class = "factor"),     year = c(1970L, 1971L, 1972L, 1973L, 1974L, 1975L, 1976L,     1977L, 1978L, 1979L, 1980L, 1981L, 1982L, 1983L, 1984L, 1985L,     1986L, 1987L, 1988L, 1970L, 1971L, 1972L, 1973L, 1974L, 1975L,     1976L, 1977L, 1978L, 1979L, 1980L, 1981L, 1982L, 1983L, 1984L,     1985L, 1986L, 1987L, 1988L, 1970L, 1971L, 1972L, 1973L, 1974L,     1975L, 1976L, 1977L, 1978L, 1979L, 1980L, 1981L, 1982L, 1983L,     1984L, 1985L, 1986L, 1987L, 1988L, 1989L, 1990L, 1991L),     X1 = c(0L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,     1L, 1L, 1L, 1L, 1L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L,     1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 0L, 0L, 0L,     0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 1L,     1L, 1L), X2 = c(0L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 1L,     1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 1L, 1L, 1L,     1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 0L,     0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 1L,     1L, 1L, 1L, 1L), X1_pre4 = c(1L, 1L, 0L, 0L, 0L, 0L, 0L,     0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,     0L, 1L, 1L, 1L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,     0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 1L,     0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), X2_pre4 = c(0L, 1L, 1L,     1L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,     0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,     0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L,     1L, 1L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), X1_pre5 = c(1L,     1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,     0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 0L, 0L, 0L, 0L,     0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,     0L, 1L, 1L, 1L, 1L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L),     X1_pre5_count = c(1L, 2L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,     0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 2L, 3L,     4L, 5L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,     0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 2L, 3L, 4L, 5L, 0L, 0L, 0L,     0L, 0L, 0L, 0L, 0L)), class = "data.frame", row.names = c(NA, -60L))         [[alternative HTML version deleted]] ______________________________________________ [hidden email] mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|

## Re: Creating a conditional lag variable in R

Open this post in threaded view
|

## Re: Creating a conditional lag variable in R

Open this post in threaded view
|

## Re: Creating a conditional lag variable in R

Open this post in threaded view
|