I am trying to calculate the cumulative standard deviation of a
matrix. I want a function that accepts a matrix and returns a matrix
of the same size where output cell (i,j) is set to the standard
deviation of input column j between rows 1 and i. NAs should be
ignored, unless cell (i,j) of the input matrix itself is NA, in which
case cell (i,j) of the output matrix should also be NA.

I could not find a built-in function, so I implemented the following
code. Unfortunately, this uses a loop that ends up being somewhat slow
for large matrices. Is there a faster built-in function or can someone
suggest a better approach?

***CODE***

cumsd <- function(mat)
{
        retval <- mat*NA
        for (i in 2:nrow(mat)) retval[i,] <- sd(mat[1:i,], na.rm=T)
        retval[is.na(mat)] <- NA
        retval
}

Thanks.

______________________________________________
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.

Reply via email to