Skip to content
Prev 314789 / 398506 Next

Refer to previous row

On 07/01/2013 8:33 AM, Paolo Donatelli wrote:
Negative indexing lets you leave out an entry, so x[-1] leaves out the 
first entry, and x[-length(x)] leaves out the last one.  To talk about 
previous entries, you need to do something about the fact that the first 
row has no previous entry.  You gave X3[1] the value 0, suggesting that 
you want to implicitly have the "zeroth" row to have the smallest 
possible value.  So

prevID <- c( -Inf, ID[-length(ID)] )
X3 <- as.numeric( ID <= prevID )

should do what you want.

Duncan Murdoch