thank you very much! Both the two methods work well for my data.
Best wishes,
Chunyu
At 2015-10-20 19:48:18, "William Dunlap" wrote:
>Or use aperm() (array index permuation):
> > array(aperm(x, c(2,1,3)), c(6,3))
> [,1] [,2] [,3]
> [1,]17 13
> [2,]4 10 16
>
Hi,
Bill was faster than me in suggesting aperm() instead of apply(),
however, his solution is still suboptimal. Try to avoid array(), and
set the dimensions directly if possible.
fn1 <- function(x) {
apply(x, 3, t)
}
fn2 <- function(x) {
array(aperm(x, c(2, 1, 3)), c(prod(dim(
Or use aperm() (array index permuation):
> array(aperm(x, c(2,1,3)), c(6,3))
[,1] [,2] [,3]
[1,]17 13
[2,]4 10 16
[3,]28 14
[4,]5 11 17
[5,]39 15
[6,]6 12 18
Bill Dunlap
TIBCO Software
wdunlap tibco.com
On Tue, Oct 20, 20
> x <- array(1:18, dim=c(3, 2, 3))
> x
, , 1
[,1] [,2]
[1,]14
[2,]25
[3,]36
, , 2
[,1] [,2]
[1,]7 10
[2,]8 11
[3,]9 12
, , 3
[,1] [,2]
[1,] 13 16
[2,] 14 17
[3,] 15 18
> apply(x, 3, t)
[,1] [,2] [,3]
[1,]17 13
4 matches
Mail list logo