[R] Creating a conditional lag variable in R
Jim Lemon
drj|m|emon @end|ng |rom gm@||@com
Sat Jul 27 05:28:14 CEST 2019
Hi Faradj,
There is a problem with your structure statement in that the hyphen
(-) following the left angle bracket (<) has been transformed into a
fancy hyphen somewhere in the process. I replaced it with an ordinary
hyphen and it worked okay. Also, your coding for "B" seems to include
the first year (1970) of "C". I have taken the liberty of correcting
this.
What you are trying to do looks a bit over-complicated to me. The
variables X1 and X2 are very redundant. Perhaps you only need to know
when the agreement was signed (1972 for country A) and when the
agreement was proposed (1970?). Thus for each country, you could
generate a delay for signing agreement X1 (A=2, B=8, C=14) and X2
(A=5, B=1, C=13). If you want to categorize the delay, I would suggest
ensuring that the category breaks are meaningful [1].
However, to answer your question, I would create X1_pre4 and
X1_pre4_count as simply the inverse of X1, then use "cumsum" to create
the year counting variable. Then change every instance of
X1_pre4_count greater than 4 to zero and also the corresponding values
of X1_pre4.
data$X1_pre4<-ifelse(data$X1,0,1)
data$X1_pre4_count[data$country=="A"]<-
cumsum(data$X1_pre4_count[data$country=="A"])
data$X1_pre4_count[data$country=="B"]<-
cumsum(data$X1_pre4_count[data$country=="B"])
data$X1_pre4_count[data$country=="c"]<-
cumsum(data$X1_pre4_count[data$country=="C"])
knockout<-which(data$X1_pre4_count == 0 | data$X1_pre4_count > 4)
data$X1_pre4_count[knockout]<-0
data$X1_pre4[knockout]<-0
Same for "X2" and "pre5".
Jim
[1] Lemon, J. (2009). On the perils of categorizing responses.
Tutorials in Quantitative Methods for Psychology, 5(1), 35-39.
On Sat, Jul 27, 2019 at 5:25 AM Faradj Koliev <faradj.g using gmail.com> wrote:
>
> 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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 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]]
>
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
More information about the R-help
mailing list