Skip to content
Prev 367376 / 398502 Next

Repeat

Thank you so much David!

But if all element of a group has '-'  did not work. In this case year
2006 an example
If all values of flag  are '-' within year  then  I wan to set as N


dat=read.table(text = "Year month flag
2001 1   Z
2001 2   -
2001 4   X
2002 1   Z
2002 2   -
2003 1   -
2003 2   Z
2004 2   Z
2005 3   Z
2005 2   -
2005 3   -

2006 1   -
2006 2   - ",  header = TRUE)

dat$new <- with(dat, ave(flag, Year, FUN=function(s){ s[s=="-"] <- NA;
           zoo::na.locf(s) }) )

Error in `[<-.factor`(`*tmp*`, i, value = integer(0)) :
  replacement has length zero
On Sat, Feb 25, 2017 at 5:43 PM, David Winsemius <dwinsemius at comcast.net> wrote: