[R] repeat matrix column within each array third dimension
Uwe Ligges
ligges at statistik.tu-dortmund.de
Wed Mar 9 20:14:43 CET 2011
See ?aperm for changing the dimensions afterwards.
Uwe Ligges
On 08.03.2011 10:14, Folkes, Michael wrote:
> Hello all,
> I'm working with a matrix that will have varying dimensions. It will populate an array such that the number of matrix columns will determine the number of 3rd dimension levels of the array. Rows will be the same for both. For this example lets say the array will have 2 columns, but that's not fixed.
>
> dim(arr)<-c(dim(mat)[1],2,dim(mat)[2])
>
> I wish to repeat each matrix column in the 3rd dimension of an array as follows in "desired appearance" below, but haven't quite got my generic array() function right. I need the equivalent of byrow=TRUE when filling in the array.
> thanks for all your help!
>
>
> mat<-matrix(1:12,ncol=3)
> mat
> desired.arr<-array(NA,dim=c(dim(mat)[1],2,dim(mat)[2]))
>
> #build array using generic code like this?
> # need something like byrow=TRUE
> array(rep(mat[1:dim(mat)[1],],each=dim(desired.arr)[2]),dim=dim(desired.arr))
>
> # array needs to visually look as follows:
> "desired appearance:"
> array(c(rep(1:4,2),rep(5:8,2),rep(9:12,2)),dim=dim(desired.arr))
>
>
> Michael Folkes
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org 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.
More information about the R-help
mailing list