# [R] Problem indexing into array

Ted Harding Ted.Harding at nessie.mcc.ac.uk
Fri Jul 18 18:54:55 CEST 2003

```> Hi Folks,
> Can anyone give me the tip I've been groping for with the
> following question:?
>
> mu: kx2x2x2 array of reals corresponding to means of k RVs
>             at the combinations of values (1,2)x(1,2)x(1,2)
>             of dichotomous variables F1,F2,F3
>     mu prints out as k rows (one for each Xi) of 8 numbers
Sorry -- this line may have misled. Suppose originally mu is
as it describes:

F3=1  F3=1  F3=1  F3=1  F3=2  F3=2  F3=2  F3=2
F2=1  F2=1  F2=2  F2=2  F2=1  F2=1  F2=2  F2=2
F1=1  F1=2  F1=1  F1=2  F1=1  F1=2  F1=1  F1=2
mu_1  1.111 1.211 1.121 1.221 1.112 1.212 1.122 1.222
mu_2  2.111 2.211 2.121 2.221 2.112 2.212 2.122 2.222
...
mu_k  k.111 k.211 k.121 k.221 k.112 k.212 k.122 k.222

and suppose it is converted into an array:

mu<-array(data=mu,dim=c(k,2,2,2))

Now the rest, below, refers to the latter form of mu.
Apologies for any confusion, and thanks for any help!

>
> M:  N x (3+k) matrix of cases. The first 3 cols are values
>             of (F1,F2,F3) as above (and the remainder are values
>             of (X1,...,Xk) but this doesn't really matter).
>
> AIM: For each row of M, find from mu the set of k means
>     mu_1,...,mu_k corresponding to the values of F1, F2, F3
>     in that row. E.g. if M[1,1:3] -> 2,1,1 I want mu[,2,1,1]
>
> BUT: Not to have to do it by mu[,M[1,1],M[1,2],M[1,3]] but more
>     like mu[,M[1,1:3]] -- except that this kind of try does not
>     work, leading to error: "incorrect number of dimensions".
>
> So:
> HOW To construct an object F from M[1,1:3] such that
>
>     mu[,F] gives mu[,2,1,1]
>
> NEXT QUESTION: If the above can be done, I'd really like to be
> able to "vectorise" it so as to get ti all at once from all the
> rows of M, rather than having to bind the results of a loop over
> the rows of M, i.e. achieve what you'd naively expect
>
>     mu[,M[,1:3]]
>
> to do (except that even mu[,M[1,1:3]] won't work).
>
> With thanks,
> Ted.
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help
>

```