Re: [R] How to generate a conditional dummy in R?

2018-05-29 Thread Jim Lemon
Hi Faradj, Yes, the function expects at least three values for each country. Glad it worked. Jim On Tue, May 29, 2018 at 10:53 PM, Faradj Koliev wrote: > Dear Jim, > > wow! It worked! Thanks a lot. > > I did as you suggested and it worked well with the real data. Although it > gave me this

Re: [R] How to generate a conditional dummy in R?

2018-05-29 Thread Faradj Koliev
Dear Jim, wow! It worked! Thanks a lot. I did as you suggested and it worked well with the real data. Although it gave me this error: Error in if (!is.na(x$Y[i])) { : argument is of length zero. For some reason the X1 produced less observations than it is in the data. But it's not a big

Re: [R] How to generate a conditional dummy in R?

2018-05-28 Thread Jim Lemon
Hi Faradj, What a problem! I think I have worked it out, but only because the result is the one you said you wanted. # the sample data frame is named fkdf Y2Xby3<-function(x) { nrows<-dim(x)[1] X<-rep(0,nrows) for(i in 1:(nrows-2)) { if(!is.na(x$Y[i])) { if(x$Y[i] == 1 &&

Re: [R] How to generate a conditional dummy in R?

2018-05-28 Thread Bert Gunter
I was not able to decipher your meaning, and your failure to garner a response so far may mean that others may have had similar difficultes. You might therefore try providing providing a **small reproducible** example of X's and Y's that show what you have and what you want to end up with to

[R] How to generate a conditional dummy in R?

2018-05-28 Thread Faradj Koliev
Hi everyone, I am trying to generate a conditional dummy variable ”X" with the following rules set X=1 if Y is =1, two years prior to the NA. [0,0,NA]. For example, if the pattern for Y is 0,0,NA then the X variable is =0 for all the two years prior to the NA. If the pattern for Y is