Skip to content
Prev 314791 / 398506 Next

R: Re: Refer to previous row

It works!

The rationale, if I have understood well, is to take my vector of N elements, ask to remove the first/last element, and replace the blank space with a given value.

Thank you all for the support!
-----Original Message-----
From: Duncan Murdoch <murdoch.duncan at gmail.com>
Date: Mon, 07 Jan 2013 13:16:50 
To: <donatellipaolo at gmail.com>
Cc: <r-help at r-project.org>
Subject: Re: [R] 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