[R] building a subscript programatically

Rolf Turner rolf.turner at xtra.co.nz
Wed Nov 2 03:23:17 CET 2011


On 02/11/11 13:43, Ernest Adrogué wrote:

<SNIP>
> Sorry for not stating my problem in a more clear way. What I want is,
> given an array of n dimensions, overwrite it by iteratating over its
> "outermost" dimension... OK, in the previous example, I would like
> to do
>
> x<- array(NA, c(2,2,2))
> for (i in 1:2) {
>      x[,,i]<- 0
> }
>
> As you can see, the index I used in the loop only works in the case of
> three-dimensional arrays, if x was two dimensional I would have had to
> write
>
> for (i in 1:2) {
>      x[,i]<- 0
> }

<SNIP>

Uhhhh, how does this differ from just setting *all* entries of x
equal to 0, e.g.:

     x[] <- 0

???

     cheers,

             Rolf Turner



More information about the R-help mailing list