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 <dresc...@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 > and provide commented, minimal, self-contained, reproducible code. > -- Joshua Wiley Ph.D. Student, Health Psychology University of California, Los Angeles http://www.joshuawiley.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.