Skip to content

How can I refer to actual (n) and previous (n-1) elements in a vector?

8 messages · Marianne Stephan, Nordlund, Dan (DSHS/RDA), Sarah Goslee +2 more

#
How about something like

v[-n] +  v[-1]

Hope this is helpful,

Dan

Daniel J. Nordlund
Washington State Department of Social and Health Services
Planning, Performance, and Accountability
Research and Data Analysis Division
Olympia, WA 98504-5204
#
Sorry for the noise.  That should have been

v[-length(v)] + v[-1]


Hope this is more helpful,

Dan

Daniel J. Nordlund
Washington State Department of Social and Health Services
Planning, Performance, and Accountability
Research and Data Analysis Division
Olympia, WA 98504-5204
#
Hi Marianne,

You have to be very careful with subsetting when doing something like that -
that's where you went wrong with your original construct. This version works:
[1]  9 14  9  2  4 12 14 11  9


But here's a more elegant way:
[1]  9 14  9  2  4 12 14 11  9

and I'm sure there are even nicer solutions.

Sarah

On Mon, Dec 6, 2010 at 12:13 PM, Marianne Stephan
<mariannestephan at hotmail.com> wrote:
1 day later