Re: [R] Creating a before-and-after variable in R

2019-10-03 Thread Richard O'Keefe
Can we do this very simply? My understanding is that you have a column where all the elements are zero except for perhaps a single one. Consider an example 0 0 1 0 0 where you want -2 -1 0 1 2. This is 1 2 3 4 5 - 3. > v <- c(0,0,1,0,0) > w <- which(v == 1) > a <- seq(along=v) - if (length(w) == 0

Re: [R] Creating a before-and-after variable in R

2019-10-03 Thread Faradj Koliev
Thank you very much for your help! All the best, Faradj > 3 okt. 2019 kl. 16:37 skrev Eric Berger : > > You can replace the last line in my first suggestion by the following two > lines > > d <- 2014 # the default (set by the user) > a$treatment <- sapply( 1:nrow(a), function(i) { b <- v[a$

Re: [R] Creating a before-and-after variable in R

2019-10-03 Thread Eric Berger
You can replace the last line in my first suggestion by the following two lines d <- 2014 # the default (set by the user) a$treatment <- sapply( 1:nrow(a), function(i) { b <- v[a$country_code[i]]; a$year[i] - ifelse(is.na(b),d,b)}) Best, Eric On Thu, Oct 3, 2019 at 5:19 PM Faradj Koliev wr

Re: [R] Creating a before-and-after variable in R

2019-10-03 Thread Faradj Koliev
Hi, I was thinking that it could simply show the negative counts. For ex: if a country hasn’t introduced the policy X, and it's in the dataset from 1982 to 2014, then the treatment variable would take a value -33 in 1982 and -1 in 2014. Best, Faradj > 3 okt. 2019 kl. 16:11 skrev Eric Ber

Re: [R] Creating a before-and-after variable in R

2019-10-03 Thread Eric Berger
Hi Faradj, What should the treatment variable be in those cases? If you want to set it to a constant y (such as y=0), you can add something like y <- 0 a$treatment[ is.na(a$treatment) ] <- y HTH, Eric On Thu, Oct 3, 2019 at 4:54 PM Faradj Koliev wrote: > Dear Eric, > > Thank you very much for

Re: [R] Creating a before-and-after variable in R

2019-10-03 Thread Faradj Koliev
Dear Eric, Thank you very much for this - it worked perfectly! A small thing: I wonder whether it’s possible to include those cases where the x is =0 for the whole study period. I have countries with x=0 for the whole period and the treatment variable is=NA for these observations. Best, Fa

Re: [R] Creating a before-and-after variable in R

2019-10-03 Thread Eric Berger
Hi Faradj, Suppose your data frame is labeled 'a'. Then the following seems to do what you want. v <- rep(NA_integer_,max(a$country_code)) v[ a$country_code[a$x==1] ] <- a$year[a$x==1] a$treatment <- sapply( 1:nrow(a), function(i) { a$year[i] - v[a$country_code[i]]}) HTH, Eric On Thu, Oct 3, 20

Re: [R] Creating a before-and-after variable in R

2019-10-03 Thread Faradj Koliev
Dear Michael Dewey, Thanks for reaching out about this. I trying again, now with plain text, and hope it works. Best, Faradj Dear R-users, I need an urgent help with the following: I have a country-year data covering the period 1982 - 2013. I want to assess how the variable X (a certai

Re: [R] Creating a before-and-after variable in R

2019-10-03 Thread Michael Dewey
Dear Faradj I am afraid your post is unreadable since this is a plain text list and you sent in HTML. Michael On 03/10/2019 12:17, Faradj Koliev wrote: Dear R-users, I need an urgent help with the following: I have a country-year data covering the period 1982 - 2013. I want to assess how t

[R] Creating a before-and-after variable in R

2019-10-03 Thread Faradj Koliev
Dear R-users, I need an urgent help with the following: I have a country-year data covering the period 1982 - 2013. I want to assess how the variable X (a certain policy) affects the Y variable. The X variable is =1 when a country introduces that policy in a specific year, otherwise =0. What