Skip to content
Prev 314794 / 398506 Next

Refer to previous row

If you first define 'n' as
   n <- seq_along(ID)[-1] # 2:length(ID)
then that code works.
[1] 0 0 1 0 1 1 0 0

You may want to put a NA at the start of the result so it has
the same length as the other columns in the data.frame.

I usually find it more convenient to leave things like ID[n]<=ID[n-1]
as logical variables instead of converting them to numbers.  Most
functions do the conversion implicitly if required and leaving them
as logicals helps me remember the meaning.

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com