An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20101206/1b2adabd/attachment.pl>
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
-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
project.org] On Behalf Of Marianne Stephan
Sent: Monday, December 06, 2010 9:13 AM
To: r-help at r-project.org
Subject: [R] How can I refer to actual (n) and previous (n-1) elements
in a vector?
Hello,
How can I apply a function on a vector that refers to actual (n) and
previous elements in the vector (e.g. n-1)?
For example:
I would like to calculate the sum of (n-1) + n for each element of a
vector and get a vector as a result.
Besides others I tried this:
v<-c(3,6,8,1,1,3,9,5,6,3)
for (i in 1:NROW(v)){a[i]<-a[i-1]+a[i]}
I would like to get this result:
9,14,9,2,4,12,14,11,9
I would greatly appreciate your help!
Marianne
[[alternative HTML version deleted]]
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
-----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- project.org] On Behalf Of Nordlund, Dan (DSHS/RDA) Sent: Monday, December 06, 2010 9:21 AM To: r-help at r-project.org Subject: Re: [R] How can I refer to actual (n) and previous (n-1) elements in a vector?
-----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- project.org] On Behalf Of Marianne Stephan Sent: Monday, December 06, 2010 9:13 AM To: r-help at r-project.org Subject: [R] How can I refer to actual (n) and previous (n-1)
elements
in a vector?
Hello,
How can I apply a function on a vector that refers to actual (n) and
previous elements in the vector (e.g. n-1)?
For example:
I would like to calculate the sum of (n-1) + n for each element of a
vector and get a vector as a result.
Besides others I tried this:
v<-c(3,6,8,1,1,3,9,5,6,3)
for (i in 1:NROW(v)){a[i]<-a[i-1]+a[i]}
I would like to get this result:
9,14,9,2,4,12,14,11,9
I would greatly appreciate your help!
Marianne
[[alternative HTML version deleted]]
How about something like v[-n] + v[-1]
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:
v<-c(3,6,8,1,1,3,9,5,6,3)
a <- numeric(length(v)-1)
for (i in 2:length(v)) {a[i-1] <- v[i-1] + v[i]}
a
[1] 9 14 9 2 4 12 14 11 9 But here's a more elegant way:
v[1:(length(v)-1)] + v[2:length(v)]
[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:
Hello,
How can I apply a function on a vector that refers to actual (n) and previous elements in the vector (e.g. n-1)?
For example:
I would like to calculate the sum of (n-1) + n for each element of a vector and get a vector as a result.
Besides others I tried this:
v<-c(3,6,8,1,1,3,9,5,6,3)
for (i in 1:NROW(v)){a[i]<-a[i-1]+a[i]}
I would like to get this result:
9,14,9,2,4,12,14,11,9
Sarah Goslee http://www.functionaldiversity.org
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20101206/c27d121c/attachment.pl>
1 day later
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20101208/c2dd9c5e/attachment.pl>
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20101208/2e88ce62/attachment.pl>
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20101208/82347c08/attachment.pl>