[R] Inflated Array
Ben Bolker
bolker at ufl.edu
Tue Jul 18 18:07:39 CEST 2006
Hadassa Brunschwig <dassybr <at> gmail.com> writes:
>
> Hi R-users!
>
> I am trying to create a what I call inflated array (maybe there is
> already some other name for that). It is an array that changes
> dinamically its dimensions, e.g. the higher the number of third
> dimensions, the more rows in the array.
base R doesn't have a data structure for this: arrays in R
must not be "ragged" (i.e., every sub-array must have the
same dimensions).
So you would need to use a list of matrices in your
example. I don't quite know the logic that you're using
to decide what goes in each sub-table.
The basic example you give could be built
by hand as follows:
list(1:4,matrix(5:12,byrow=TRUE,ncol=4),
matrix(13:24,byrow=TRUE,ncol=4))
if you had used scan() or something else to
get a long, flat vector (x) and also had
a vector (v) that indicated the number of
rows in each sub-table:
x <- 1:24
nrow <- c(1,2,3)
ncol <- 4
ind <- rep(1:length(nrow),ncol*nrow)
lapply(split(x,ind),
matrix,ncol=ncol,byrow=TRUE)
seems to work.
cheers
Ben Bolker
More information about the R-help
mailing list