Skip to content
Back to formatted view

Raw Message

Message-ID: <x28xz1mvh8.fsf@turmalin.kubism.ku.dk>
Date: 2005-08-16T21:18:27Z
From: Peter Dalgaard
Subject: how to reshape an array avoiding for loops
In-Reply-To: <171836925.20050816131204@eimb.ru>

Wladimir Eremeev <wl at eimb.ru> writes:

> Dear r-help,
> 
>   I have an array a1 with dimensions [1:660,1:65,1:25]
>   I would like the first dimension to be the last one.
>   That is I want and array [1:65,1:25,1:660]
> 
>   The only way to do this, I know, is
>   
>   tmp.a<-array(dim=dim(a1)[c(2,3,1)])
>   for(i in 1:dim(a1)[1]) tmp.a[,,i]<-a1[i,,]
>   a1<-tmp.a
>   rm(tmp.a)
> 
>   
>   Is it possible to avoid 'for' loop here?

aperm() is your friend.

-- 
   O__  ---- Peter Dalgaard             ??ster Farimagsgade 5, Entr.B
  c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
 (*) \(*) -- University of Copenhagen   Denmark          Ph:  (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)                  FAX: (+45) 35327907