Marianne Stephan
2010-Dec-06 17:13 UTC
[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]]
Nordlund, Dan (DSHS/RDA)
2010-Dec-06 17:21 UTC
[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] 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
Nordlund, Dan (DSHS/RDA)
2010-Dec-06 17:27 UTC
[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 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
Sarah Goslee
2010-Dec-06 17:29 UTC
[R] How can I refer to actual (n) and previous (n-1) elements in a vector?
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 functionaldiversity.org
Dennis Murphy
2010-Dec-07 02:26 UTC
[R] How can I refer to actual (n) and previous (n-1) elements in a vector?
Hi: This is called a 'rolling sum', for which there is a very convenient function called rollapply() in the zoo package. You first need to convert v to a zoo object (in this case, an indexed vector): library(zoo)> rollapply(zoo(v), 2, FUN = sum)1 2 3 4 5 6 7 8 9 9 14 9 2 4 12 14 11 9 The second argument is the width of the window to which the function is to be applied - in this case, you want a window of width two and a sum function. HTH, Dennis On Mon, Dec 6, 2010 at 9:13 AM, Marianne Stephan < mariannestephan@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 > > > I would greatly appreciate your help! > Marianne > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@r-project.org mailing list > stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]
Marianne Stephan
2010-Dec-08 14:29 UTC
[R] How can I calculate the median for each factor combination?
Hello everybody, I would like to calculate the median for each factor combination, with only one value per factor combination as an output. Could anybody help me? For example: # make table g<-1:2 group<-rep(g, each=5) session<-c(1,1,2,2,2,1,1,1,2,2) rt<-seq(length=10,300, 800) rt<-round(rt, digits=2) table<-data.frame(group, session, rt) table group session rt 1 1 1 300.00 2 1 1 355.56 3 1 2 411.11 4 1 2 466.67 5 1 2 522.22 6 2 1 577.78 7 2 1 633.33 8 2 1 688.89 9 2 2 744.44 10 2 2 800.00 Besides others I tried the following: median<-ave(rt, group, session, FUN=median) median<-frameApply(table,by=c("group", "session"), on="rt", fun=median) I would appreciate your help a lot. Marianne [[alternative HTML version deleted]]
Reasonably Related Threads
- SAS to R: I would like to replicate a statistical analysis performed in SAS in R.
- Help in determining the formula for a mixed model analysis
- gsub patterns from vector elements w/out loop?
- cross tabulate variables by subject id
- pass nrow(x) to dots in function(x){plot(x,...)}