Skip to content

Result depends on previous result; easy with a loop; but without a loop?

3 messages · Roland Rau, Ravi Varadhan

#
Dear all,

I am pretty sure that this has been discussed before. Unfortunately, I 
can not find anything in the archives -- probably because I am 
"RSiteSearching" for the wrong terms. If I remember correctly, I think I 
even asked this question a few years ago. But I cannot even find this.

The basic problem is that a result depends on a previous result. This is 
easy with a loop--but how can I do this without a loop?

Lets give an example:

initial.matrix <- rbind(rep(1,4), matrix(0,ncol=4,nrow=5))
the.other.matrix <- matrix(runif(20), ncol=4, nrow=5)

the initial matrix should be filled according to the following 
(pseudo-code) rule:
if (row==1) initial.matrix[1,] <- 1
if (row>1) initial.matrix[x,] <- initial.matrix[x-1,] * 
the.other.matrix[x-1,]

as I said this is easy to do with a loop:
for (i in 2:(nrow(initial.matrix))) {
   initial.matrix[i,] <- initial.matrix[i-1,]*the.other.matrix[i-1,]
}
initial.matrix

But how can I do this without a loop?

Thank you already in advance!
Roland
#
Dear all,

in the meantime, I found a solution -- thank to a suggestion sent by 
Mark Leeds to me off-list.

All the best,
Roland

set.seed(1234)
initial.matrix <- rbind(rep(1,4), matrix(0,ncol=4,nrow=5))
the.other.matrix <- matrix(runif(20), ncol=4, nrow=5)
for (i in 2:(nrow(initial.matrix))) {
   initial.matrix[i,] <- initial.matrix[i-1,]*the.other.matrix[i-1,]
}
### that is how it should look like:
initial.matrix


### this is Mark's suggestion (if I understood it correctly)
initial.matrix2 <- rbind(rep(1,4), matrix(1,ncol=4,nrow=5))
initial.matrix2[-1,] <- sapply(1:ncol(initial.matrix2),
                                function(.col) {
cumprod(initial.matrix2[-(nrow(initial.matrix2)),.col]
                                          * the.other.matrix[,.col])
                                }
                                )
## and it works!!!
initial.matrix2
if (all(initial.matrix==initial.matrix2)) cat("Good\n") else cat("Bad\n")
## yes, I know, such comparisons of floats are notoriously problematic
Roland Rau wrote:
#
Roland,

You can test for the "sameness" of floating-point results as follows:

all.equal(initial.matrix, initial.matrix2)

By default, it uses a tolerance = .Machine$double.eps ^ 0.5 (roughly,
1.e-08).  You can decrease this if you want a more stringent test for
sameness.

all.identical(initial.matrix, initial.matrix2) 

This tests for "identicality", which, of course, is not appropriate for
floating point computations.

Ravi.

----------------------------------------------------------------------------
-------

Ravi Varadhan, Ph.D.

Assistant Professor, The Center on Aging and Health

Division of Geriatric Medicine and Gerontology 

Johns Hopkins University

Ph: (410) 502-2619

Fax: (410) 614-9625

Email: rvaradhan at jhmi.edu

Webpage:  http://www.jhsph.edu/agingandhealth/People/Faculty/Varadhan.html

 

----------------------------------------------------------------------------
--------


-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On
Behalf Of Roland Rau
Sent: Friday, December 14, 2007 2:33 PM
To: 'r-help at stat.math.ethz.ch'
Subject: Re: [R] Result depends on previous result; easy with a loop; but
without a loop?

Dear all,

in the meantime, I found a solution -- thank to a suggestion sent by 
Mark Leeds to me off-list.

All the best,
Roland

set.seed(1234)
initial.matrix <- rbind(rep(1,4), matrix(0,ncol=4,nrow=5))
the.other.matrix <- matrix(runif(20), ncol=4, nrow=5)
for (i in 2:(nrow(initial.matrix))) {
   initial.matrix[i,] <- initial.matrix[i-1,]*the.other.matrix[i-1,]
}
### that is how it should look like:
initial.matrix


### this is Mark's suggestion (if I understood it correctly)
initial.matrix2 <- rbind(rep(1,4), matrix(1,ncol=4,nrow=5))
initial.matrix2[-1,] <- sapply(1:ncol(initial.matrix2),
                                function(.col) {
cumprod(initial.matrix2[-(nrow(initial.matrix2)),.col]
                                          * the.other.matrix[,.col])
                                }
                                )
## and it works!!!
initial.matrix2
if (all(initial.matrix==initial.matrix2)) cat("Good\n") else cat("Bad\n")
## yes, I know, such comparisons of floats are notoriously problematic
Roland Rau wrote:
http://www.R-project.org/posting-guide.html
______________________________________________
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.