Dear R-users, I would like to apply a function (more precisely sd()) over the third dimension of a three-dimension array. The function apply would be interesting but the chosen function can only be applied on the rows and columns of the array according to the help file. I can use a loop to cut the array in matrices and then use apply for each replication, but it's not very nice. A small example: a <- array(runif(1200),c(3,4,100)) b <- matrix(NA,3,4) for(j in 1:3){ b[j,] <- apply(a[j,,],1,sd) } Is there someting better than the use of a loop ? Thanks
Hi, apply(aperm(a), 2, sd) -- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O On 24/08/07, Florent Bresson <f_bresson@yahoo.fr> wrote:> > Dear R-users, > > I would like to apply a function (more precisely sd()) over the third > dimension of a three-dimension array. The function apply would be > interesting but the chosen function can only be applied on the rows and > columns of the array according to the help file. I can use a loop to cut the > array in matrices and then use apply for each replication, but it's not very > nice. A small example: > > a <- array(runif(1200),c(3,4,100)) > b <- matrix(NA,3,4) > for(j in 1:3){ b[j,] <- apply(a[j,,],1,sd) } > > Is there someting better than the use of a loop ? > > Thanks > > ______________________________________________ > R-help@stat.math.ethz.ch 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. >[[alternative HTML version deleted]]
Hi, Alternatively: apply( a, c(1,2), sd ) Cheers, Tsjerk On 8/24/07, Florent Bresson <f_bresson at yahoo.fr> wrote:> Dear R-users, > > I would like to apply a function (more precisely sd()) over the third dimension of a three-dimension array. The function apply would be interesting but the chosen function can only be applied on the rows and columns of the array according to the help file. I can use a loop to cut the array in matrices and then use apply for each replication, but it's not very nice. A small example: > > a <- array(runif(1200),c(3,4,100)) > b <- matrix(NA,3,4) > for(j in 1:3){ b[j,] <- apply(a[j,,],1,sd) } > > Is there someting better than the use of a loop ? > > Thanks > > ______________________________________________ > R-help at stat.math.ethz.ch 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. >-- Tsjerk A. Wassenaar, Ph.D. Junior UD (post-doc) Biomolecular NMR, Bijvoet Center Utrecht University Padualaan 8 3584 CH Utrecht The Netherlands P: +31-30-2539931 F: +31-30-2537623