Re: [R] Creating a conditional lag variable in R

2019-07-27 Thread Faradj Koliev
Thank you all. I now have the right solution for this (perhaps of interest to some): check_pre <- function(idx, k) { pre_vec <- sapply(1:length(idx), function(x) +any(idx[x:(pmin(x + k, length(idx)))] %in% 1)); pre_vec[idx == 1] <- 0; return(pre_vec) } df %>% group_by(country) %>% mutate(

Re: [R] Creating a conditional lag variable in R

2019-07-27 Thread Faradj Koliev
Peter Dalgaard, Thanks for this. I’ll try to think of ways to apply this logic. At the moment, I’m trying to do this with “mutate” using dplyr package. But it’s not easy.. > On 27 Jul 2019, at 10:33, peter dalgaard wrote: > > Some pointers (not tested, may contain blunders...) > > (a) you

Re: [R] Creating a conditional lag variable in R

2019-07-27 Thread peter dalgaard
Some pointers (not tested, may contain blunders...) (a) you likely need some sort of split-operate-unsplit construct, by country. E.g., myfun <- function(d) {operate on data frame with only one country} ll <- split(data, data$country) ll.new <- lapply(ll, myfun) data.new <- unsplit(ll.n

Re: [R] Creating a conditional lag variable in R

2019-07-27 Thread Faradj Koliev
Re-post, now in *plain text*. 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 agree

Re: [R] Creating a conditional lag variable in R

2019-07-26 Thread Jim Lemon
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 y

Re: [R] Creating a conditional lag variable in R

2019-07-26 Thread Bert Gunter
Because you posted in HTML, your example got mangled and resulted in an error. Re-post in *plain text* please (making sure that you cut and paste correctly) Bert Gunter "The trouble with having an open mind is that people keep coming along and sticking things into it." -- Opus (aka Berkeley Breat

[R] Creating a conditional lag variable in R

2019-07-26 Thread Faradj Koliev
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