Re: [R] Formula that includes previous row values
Dear Pele, Probably not the best way to proceed but it works: X<-read.table(textConnection("ID X2 1.001.00 2.000.00 3.001.00 4.003058 5.000.00 6.006.00"),header=TRUE) closeAllConnections() X x3<-0 for(i in 2:(nrow(X)+1)) x3<-c(x3, X$X2[i-1]+0.24*x3[i-1]) X$x3<-x3[-1] X HTH, Jorge On Mon, Feb 23, 2009 at 3:59 PM, Pele wrote: > > Hi R users, > > Is there an easy way in R to generate the results table below using table 1 > and the formula (simplified version of the real problem)? It would be easy > if I knew the R equivalent of SAS's retain function, but could not find > one. > > Thanks in Advance for any help! > > table1: > > ID X2 X3 > 1.001.00 0 > 2.000.00 > 3.001.00 > 4.003058 > 5.000.00 > 6.006.00 > > Formula: X3 = x2 + (.24 * x3) > > where the values in the x3 column of the result table are retained from > previous x3 rows.. Also the first x3 value is initialized to 0 to start > > e.g. >for ID=1 we have 1 + .24(0)= 1.00 >for ID=2 we have 0 + .24(1)= 0.24 >for ID=3 we have 1 + .24(.24) = 1.06 >for ID=4 we have 3058 + .24(1.06) = 3058.25 >etc. > > Results: > ID X2 x3 > 1.001.001.00 > 2.000.000.24 > 3.001.001.06 > 4.0030583058.25 > 5.000.00733.98 > 6.006.00182.16 > -- > View this message in context: > http://www.nabble.com/Formula-that-includes-previous-row-values-tp22170010p22170010.html > Sent from the R help mailing list archive at Nabble.com. > > __ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Formula that includes previous row values
Hi Jorge - many thanks for you suggestion, but I am looking for a way where I don't have to use a loop. I meant to include that in my description. Thanks again! Jorge Ivan Velez wrote: > > Dear Pele, > Probably not the best way to proceed but it works: > > X<-read.table(textConnection("ID X2 > 1.001.00 > 2.000.00 > 3.001.00 > 4.003058 > 5.000.00 > 6.006.00"),header=TRUE) > closeAllConnections() > X > > x3<-0 > for(i in 2:(nrow(X)+1)) x3<-c(x3, X$X2[i-1]+0.24*x3[i-1]) > X$x3<-x3[-1] > X > > HTH, > > Jorge > > > On Mon, Feb 23, 2009 at 3:59 PM, Pele wrote: > >> >> Hi R users, >> >> Is there an easy way in R to generate the results table below using table >> 1 >> and the formula (simplified version of the real problem)? It would be >> easy >> if I knew the R equivalent of SAS's retain function, but could not find >> one. >> >> Thanks in Advance for any help! >> >> table1: >> >> ID X2 X3 >> 1.001.00 0 >> 2.000.00 >> 3.001.00 >> 4.003058 >> 5.000.00 >> 6.006.00 >> >> Formula: X3 = x2 + (.24 * x3) >> >> where the values in the x3 column of the result table are retained from >> previous x3 rows.. Also the first x3 value is initialized to 0 to start >> >> e.g. >>for ID=1 we have 1 + .24(0)= 1.00 >>for ID=2 we have 0 + .24(1)= 0.24 >>for ID=3 we have 1 + .24(.24) = 1.06 >>for ID=4 we have 3058 + .24(1.06) = 3058.25 >>etc. >> >> Results: >> ID X2 x3 >> 1.001.001.00 >> 2.000.000.24 >> 3.001.001.06 >> 4.0030583058.25 >> 5.000.00733.98 >> 6.006.00182.16 >> -- >> View this message in context: >> http://www.nabble.com/Formula-that-includes-previous-row-values-tp22170010p22170010.html >> Sent from the R help mailing list archive at Nabble.com. >> >> __ >> R-help@r-project.org mailing list >> https://stat.ethz.ch/mailman/listinfo/r-help >> PLEASE do read the posting guide >> http://www.R-project.org/posting-guide.html >> and provide commented, minimal, self-contained, reproducible code. >> > > [[alternative HTML version deleted]] > > __ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > > -- View this message in context: http://www.nabble.com/Formula-that-includes-previous-row-values-tp22170010p22170878.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Formula that includes previous row values
Here's a way without a loop: x <- read.table(textConnection("ID X2 1.001.00 2.000.00 3.001.00 4.003058 5.000.00 6.006.00"),header=TRUE) closeAllConnections() x$X3 <- append(x$X2, 0, 0)[-nrow(x)] x$X4 <- as.matrix(x[,2:3]) %*% c(1, 0.24) > x ID X2 X3 X4 1 1101.00 2 2010.24 3 3101.00 4 4 30581 3058.24 5 50 3058 733.92 6 6667.44 -Christos > -Original Message- > From: r-help-boun...@r-project.org > [mailto:r-help-boun...@r-project.org] On Behalf Of Pele > Sent: Monday, February 23, 2009 4:48 PM > To: r-help@r-project.org > Subject: Re: [R] Formula that includes previous row values > > > Hi Jorge - many thanks for you suggestion, but I am looking > for a way where I don't have to use a loop. I meant to > include that in my description. > > Thanks again! > > > Jorge Ivan Velez wrote: > > > > Dear Pele, > > Probably not the best way to proceed but it works: > > > > X<-read.table(textConnection("ID X2 > > 1.001.00 > > 2.000.00 > > 3.001.00 > > 4.003058 > > 5.000.00 > > 6.006.00"),header=TRUE) > > closeAllConnections() > > X > > > > x3<-0 > > for(i in 2:(nrow(X)+1)) x3<-c(x3, X$X2[i-1]+0.24*x3[i-1]) > X$x3<-x3[-1] > > X > > > > HTH, > > > > Jorge > > > > > > On Mon, Feb 23, 2009 at 3:59 PM, Pele wrote: > > > >> > >> Hi R users, > >> > >> Is there an easy way in R to generate the results table > below using > >> table > >> 1 > >> and the formula (simplified version of the real problem)? > It would > >> be easy if I knew the R equivalent of SAS's retain function, but > >> could not find one. > >> > >> Thanks in Advance for any help! > >> > >> table1: > >> > >> ID X2 X3 > >> 1.001.00 0 > >> 2.000.00 > >> 3.001.00 > >> 4.003058 > >> 5.000.00 > >> 6.006.00 > >> > >> Formula: X3 = x2 + (.24 * x3) > >> > >> where the values in the x3 column of the result table are retained > >> from previous x3 rows.. Also the first x3 value is > initialized to 0 > >> to start > >> > >> e.g. > >>for ID=1 we have 1 + .24(0)= 1.00 > >>for ID=2 we have 0 + .24(1)= 0.24 > >>for ID=3 we have 1 + .24(.24) = 1.06 > >>for ID=4 we have 3058 + .24(1.06) = 3058.25 > >>etc. > >> > >> Results: > >> ID X2 x3 > >> 1.001.001.00 > >> 2.000.000.24 > >> 3.001.001.06 > >> 4.0030583058.25 > >> 5.000.00733.98 > >> 6.006.00182.16 > >> -- > >> View this message in context: > >> > http://www.nabble.com/Formula-that-includes-previous-row-values-tp221 > >> 70010p22170010.html Sent from the R help mailing list archive at > >> Nabble.com. > >> > >> __ > >> R-help@r-project.org mailing list > >> https://stat.ethz.ch/mailman/listinfo/r-help > >> PLEASE do read the posting guide > >> http://www.R-project.org/posting-guide.html > >> and provide commented, minimal, self-contained, reproducible code. > >> > > > > [[alternative HTML version deleted]] > > > > __ > > R-help@r-project.org mailing list > > https://stat.ethz.ch/mailman/listinfo/r-help > > PLEASE do read the posting guide > > http://www.R-project.org/posting-guide.html > > and provide commented, minimal, self-contained, reproducible code. > > > > > > -- > View this message in context: > http://www.nabble.com/Formula-that-includes-previous-row-value > s-tp22170010p22170878.html > Sent from the R help mailing list archive at Nabble.com. > > __ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > > __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Formula that includes previous row values
How about: x3 <- cumsum( x2* 0.24^(5:0) ) / 0.24^(5:0) with the 5 replaced by the length -1 for the more general case. -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.s...@imail.org 801.408.8111 > -Original Message- > From: r-help-boun...@r-project.org [mailto:r-help-boun...@r- > project.org] On Behalf Of Pele > Sent: Monday, February 23, 2009 1:59 PM > To: r-help@r-project.org > Subject: [R] Formula that includes previous row values > > > Hi R users, > > Is there an easy way in R to generate the results table below using > table 1 > and the formula (simplified version of the real problem)? It would be > easy > if I knew the R equivalent of SAS's retain function, but could not find > one. > > Thanks in Advance for any help! > > table1: > > IDX2 X3 > 1.00 1.00 0 > 2.00 0.00 > 3.00 1.00 > 4.00 3058 > 5.00 0.00 > 6.00 6.00 > > Formula: X3 = x2 + (.24 * x3) > > where the values in the x3 column of the result table are retained from > previous x3 rows.. Also the first x3 value is initialized to 0 to start > > e.g. > for ID=1 we have 1 + .24(0)= 1.00 > for ID=2 we have 0 + .24(1)= 0.24 > for ID=3 we have 1 + .24(.24) = 1.06 > for ID=4 we have 3058 + .24(1.06) = 3058.25 > etc. > > Results: > IDX2 x3 > 1.00 1.001.00 > 2.00 0.000.24 > 3.00 1.001.06 > 4.00 30583058.25 > 5.00 0.00733.98 > 6.00 6.00182.16 > -- > View this message in context: http://www.nabble.com/Formula-that- > includes-previous-row-values-tp22170010p22170010.html > Sent from the R help mailing list archive at Nabble.com. > > __ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting- > guide.html > and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Formula that includes previous row values
Hi Greg - this is perfect - Thank You! Also, thanks to everyone for the other suggestions Greg Snow-2 wrote: > > How about: > > x3 <- cumsum( x2* 0.24^(5:0) ) / 0.24^(5:0) > > with the 5 replaced by the length -1 for the more general case. > > -- > Gregory (Greg) L. Snow Ph.D. > Statistical Data Center > Intermountain Healthcare > greg.s...@imail.org > 801.408.8111 > > >> -Original Message- >> From: r-help-boun...@r-project.org [mailto:r-help-boun...@r- >> project.org] On Behalf Of Pele >> Sent: Monday, February 23, 2009 1:59 PM >> To: r-help@r-project.org >> Subject: [R] Formula that includes previous row values >> >> >> Hi R users, >> >> Is there an easy way in R to generate the results table below using >> table 1 >> and the formula (simplified version of the real problem)? It would be >> easy >> if I knew the R equivalent of SAS's retain function, but could not find >> one. >> >> Thanks in Advance for any help! >> >> table1: >> >> ID X2 X3 >> 1.00 1.00 0 >> 2.00 0.00 >> 3.00 1.00 >> 4.00 3058 >> 5.00 0.00 >> 6.00 6.00 >> >> Formula: X3 = x2 + (.24 * x3) >> >> where the values in the x3 column of the result table are retained from >> previous x3 rows.. Also the first x3 value is initialized to 0 to start >> >> e.g. >> for ID=1 we have 1 + .24(0)= 1.00 >> for ID=2 we have 0 + .24(1)= 0.24 >> for ID=3 we have 1 + .24(.24) = 1.06 >> for ID=4 we have 3058 + .24(1.06) = 3058.25 >> etc. >> >> Results: >> ID X2 x3 >> 1.00 1.001.00 >> 2.00 0.000.24 >> 3.00 1.001.06 >> 4.00 30583058.25 >> 5.00 0.00733.98 >> 6.00 6.00182.16 >> -- >> View this message in context: http://www.nabble.com/Formula-that- >> includes-previous-row-values-tp22170010p22170010.html >> Sent from the R help mailing list archive at Nabble.com. >> >> __ >> R-help@r-project.org mailing list >> https://stat.ethz.ch/mailman/listinfo/r-help >> PLEASE do read the posting guide http://www.R-project.org/posting- >> guide.html >> and provide commented, minimal, self-contained, reproducible code. > > __ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > > -- View this message in context: http://www.nabble.com/Formula-that-includes-previous-row-values-tp22170010p22173033.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.