This should work. (I often forget to name the FUN argument and ave
then complains with a cryptic error message.)
#Wrong dfrm$Y <- ave( dfrm$Y, dfrm$X, FUN=function(x) c(NA, x) )
Lacking a proper grouping factor to mark distinct categories of time sequences, I decided to cue off X==1:
dfrm$Y <- ave( dfrm$Y, cumsum(dfrm$X==1), FUN=function(x) c(NA, x[-length(x)]) )