[R] Speeding up "accumulation" code in large matrix calc?
Petr Savicky
savicky at cs.cas.cz
Fri Feb 24 20:02:30 CET 2012
On Fri, Feb 24, 2012 at 08:59:44AM -0800, robertfeldt wrote:
> Hi,
>
> I have R code like so:
>
> num.columns.back.since.last.occurence <- function(m, outcome) {
> nrows <- dim(m)[1];
> ncols <- dim(m)[2];
> res <- matrix(rep.int(0, nrows*ncols), nrow=nrows);
> for(row in 1:nrows) {
> for(col in 2:ncols) {
> res[row,col] <- if(m[row,col-1]==outcome) {0} else {1+res[row,col-1]}
> }
> }
> res;
> }
>
> but on the very large matrices I apply this the execution times are a
> problem. I would appreciate any help to rewrite this with more
> "standard"/native R functions to speed things up.
Hi.
If the number of rows is large and the number of columns is not,
then try the following.
# random matrix
A <- matrix((runif(49) < 0.2) + 0, nrow=7)
outcome <- 1
# transformation
B <- array(0, dim=dim(A))
curr <- B[, 1]
for (i in seq.int(from=2, length=ncol(A)-1)) {
curr <- ifelse (A[, i-1] == outcome, 0, 1 + curr)
B[, i] <- curr
}
# verify
all(num.columns.back.since.last.occurence(A, 1) == B)
[1] TRUE
Hope this helps.
Petr Savicky.
More information about the R-help
mailing list