[R] Creating 250 submatrices from a large single matrix with 2500 variables using loops
Tsjerk Wassenaar
tsjerkw at gmail.com
Wed Mar 14 10:45:53 CET 2012
Hi Lazarus,
You should use apply, not sapply.
> a =matrix(1:12, nrow=2,ncol=6,byrow=TRUE)
> b = array(a,c(2,2,3));b
> colMeans(b)
>
> f1=function(x) sum(x)/length(row(a.df[,1:2]))
> y3 <- apply(b, 1, f1)
>
> It only gives one matrix with only two values, I expected 3 values.
To apply the function on each submatrix, you have to apply it over the
third index:
y3 <- apply(b, 3, f1)
Cheers,
Tsjerk
--
Tsjerk A. Wassenaar, Ph.D.
post-doctoral researcher
Molecular Dynamics Group
* Groningen Institute for Biomolecular Research and Biotechnology
* Zernike Institute for Advanced Materials
University of Groningen
The Netherlands
More information about the R-help
mailing list