Marcus Drescher
2010-Aug-27 17:17 UTC
[R] How to calc ratios base on current and previous row?
Hi all, I want to calculate in each row a ratio based on number in the current row and the previous row. Is there a way to do this without for-loops because that is extremely slow. A B [1] 2 2 [2] 2 3 [3] 4 5,5 ... B2 = A2 + 0.5*B1 Thanks in advance. Best Marcus
Bert Gunter
2010-Aug-27 17:27 UTC
[R] How to calc ratios base on current and previous row?
Your question is unclear. If the calculation of B[3] now depends on the newly calculated value of B[2], then the answer is no: you must loop. If B[3] is based on the original B[2] value, then the answer is yes, and the solution is just a matter of simple indexing, which I leave as an exercise. -- Bert On Fri, Aug 27, 2010 at 10:17 AM, Marcus Drescher <drescher@tum.de> wrote:> Hi all, > > I want to calculate in each row a ratio based on number in the current row > and the previous row. > Is there a way to do this without for-loops because that is extremely slow. > > A B > [1] 2 2 > [2] 2 3 > [3] 4 5,5 > ... > > B2 = A2 + 0.5*B1 > > > Thanks in advance. > Best > Marcus > > ______________________________________________ > 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<http://www.r-project.org/posting-guide.html> > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]
(Ted Harding)
2010-Aug-27 17:34 UTC
[R] How to calc ratios base on current and previous row?
On 27-Aug-10 17:17:50, Marcus Drescher wrote:> Hi all, > I want to calculate in each row a ratio based on number in the current > row and the previous row. > Is there a way to do this without for-loops because that is extremely > slow. > > A B > [1] 2 2 > [2] 2 3 > [3] 4 5,5 > ... > > B2 = A2 + 0.5*B1 > > Thanks in advance. > Best > MarcusIf n is the total number of rows, then : B[(2:n)] <- A[(2:n)] + 0.5*B[1:(n-1)] should work! This assumes that A and B have been extracted as vectors, or can be referred to separately as in a dataframe (say D), like D$B[(2:n)] <- D$A[(2:n)] + 0.5*D$B[1:(n-1)] Ted. -------------------------------------------------------------------- E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk> Fax-to-email: +44 (0)870 094 0861 Date: 27-Aug-10 Time: 18:34:27 ------------------------------ XFMail ------------------------------
Joshua Wiley
2010-Aug-27 19:12 UTC
[R] How to calc ratios base on current and previous row?
Hi Marcus, I am guessing you are thinking in terms of Excel, where in column B, you could enter the formula =(A2 + 0.5*B1) and just drag it down however many cells you wanted. In R, it would be expressed a bit differently. If k indexes your rows, I believe you want: B[k] = A[k] + 0.5 * B[k - 1] Unfortunately, if this is indeed what you mean, then as Bert said "If the calculation of B[3] now depends on the newly calculated value of B[2], then the answer is no: you must loop." If calculating B is just some middle step to a larger goal, there may be another way to your end goal that does not require a loop. For instance, suppose all you are really interested in is a single value of B (perhaps the last one), you can use this formula where k = the row number of the value of B you want: A[k] + sum(A[1:(k - 1)] * (0.5 ^ ((k - 1):1))) Best wishes, Josh On Fri, Aug 27, 2010 at 10:17 AM, Marcus Drescher <drescher at tum.de> wrote:> Hi all, > > I want to calculate in each row a ratio based on number in the current row and the previous row. > Is there a way to do this without for-loops because that is extremely slow. > > ? ? ? ?A ? ? ? B > [1] ? ? 2 ? ? ? 2 > [2] ? ? 2 ? ? ? 3 > [3] ? ? 4 ? ? ? 5,5 > ... > > B2 = A2 + 0.5*B1 > > > Thanks in advance. > Best > Marcus > > ______________________________________________ > R-help at 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. >-- Joshua Wiley Ph.D. Student, Health Psychology University of California, Los Angeles http://www.joshuawiley.com/
For this particular case there is a nice shortcut (there is still looping , but it is internal and quick): a <- c(2,2,4, sample(1:10, 97, TRUE) ) b <- cumsum( (1/2)^(99:0)*a )/( (1/2)^(99:0) ) ## compare bb <- numeric(100) bb[1] <- a[1] for( i in 2:100 ) { bb[i] <- 1/2 * bb[i-1] + a[i] } all.equal( b, bb ) you will get some slight differences due to rounding error from the 2 methods. this can be generalized to some degree, but if things get too complicated then you may need to loop. Hope this helps, -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.snow at imail.org 801.408.8111> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of Marcus Drescher > Sent: Friday, August 27, 2010 11:18 AM > To: r-help at r-project.org > Subject: [R] How to calc ratios base on current and previous row? > > Hi all, > > I want to calculate in each row a ratio based on number in the current > row and the previous row. > Is there a way to do this without for-loops because that is extremely > slow. > > A B > [1] 2 2 > [2] 2 3 > [3] 4 5,5 > ... > > B2 = A2 + 0.5*B1 > > > Thanks in advance. > Best > Marcus > > ______________________________________________ > R-help at 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.