Skip to content
Prev 314784 / 398503 Next

Refer to previous row

On Jan 7, 2013, at 5:33 AM, Paolo Donatelli wrote:

            
Something like (untested):

   dfrm$X3 <- c(0, as.numeric( diff(dfrm$ID) <= 0 ) )

That might be faster than this sort of untested strategy:

    ...   <- with(dfrm, c( 0 , as.numeric( ID[2:nrow(dfrm)] <= ID[1: 
(nrow(dfrm)-1] ) ) )

In my newbie days I thought a function named `lag` would do it, but  
discovered it was only working on ts-class objects.
An explicit sequence rather than using mathematical notation is  
needed. And if you are using dataframes, you should not be using  
`attach`. That X3 would not be constructed in the dataframe.