Skip to content
Prev 309779 / 398506 Next

Replacing NAs in long format

ave() or split<-() can make that easier to write, although it
may take some time to internalize the idiom.  E.g.,

  > flag <- rep(NA, nrow(dat2)) # add as.integer if you prefer 1,0 over TRUE,FALSE
  > split(flag, dat2$idr) <- lapply(split(dat2, dat2$idr), function(d)with(d, any(schyear<=5 & year==0))) 
  > data.frame(dat2, flag)
    idr schyear year  flag
  1   1       4   -1  TRUE
  2   1       5    0  TRUE
  3   1       6    1  TRUE
  4   1       7    2  TRUE
  5   2       9    0 FALSE
  6   2      10    1 FALSE
  7   2      11    2 FALSE
or
  > ave(seq_len(nrow(dat2)), dat2$idr, FUN=function(i)with(dat2[i,], any(schyear<=5 & year==0))) 
  [1] 1 1 1 1 0 0 0
  > flag <- ave(seq_len(nrow(dat2)), dat2$idr, FUN=function(i)with(dat2[i,], any(schyear<=5 & year==0))) 
  > data.frame(dat2, flag)
    idr schyear year flag
  1   1       4   -1    1
  2   1       5    0    1
  3   1       6    1    1
  4   1       7    2    1
  5   2       9    0    0
  6   2      10    1    0
  7   2      11    2    0

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com