# [R] cumsum over a list or an array

Stephane DRAY stephane.dray at umontreal.ca
Sat Oct 2 00:37:24 CEST 2004

```Hello list,

my question is related to svd of a matrix:

b=matrix(rnorm(50),10,5)
mysvd=svd(b)

I would like to compute each xi where xi = di* ui %*% t(vi). I do it by :

xlist=sapply(1:ncol(b), function(x1,y)
y\$d[x1]*y\$u[,x1]%*%t(y\$v[,x1]),y=mysvd,simplify=F) # result is a list

xarray=array(sapply(1:ncol(b), function(x1,y)
y\$d[x1]*y\$u[,x1]%*%t(y\$v[,x1]),y=mysvd),c(nrow(b),ncol(b),ncol(b))) #
result is an array

Now i would like to compute cumulative sum:

y1=x1 # y[,,1]
y2=x1+x2 # y[,,2]
...

I have try to do it with apply without succes:

y=apply(xarray,c(1,2),cumsum)

The results are good but not in the format that I want. I could modify the
results to modify the format but I am sure that it exists another faster way.

Is it possible to do the same on the list ?

Sincerely

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