In response to @ttmaccer answer: does this work because of a (slightly) obscure paragraph in ?"["
, Which reads:
When indexing arrays by '[' a single argument 'i' can be a matrix with as many columns as there are dimensions of 'x'; the result is then a vector with elements corresponding to the sets of indices in each row of 'i'.
The effect of using t(ii)
in
ii <- c(5,5,5,5) a[t(ii)]
consists in converting ii
to a 1x4 matrix, which [
interprets as a matrix as described above; a[matrix(ii,nrow=1)]
will be more explicit, but less compact.
The good thing about this approach (besides avoiding the magically seeming aspects of do.call
) is that it works in parallel for more than one set of indices, as in
jj <- matrix(c(5,5,5,5, 6,6,6,6),byrow=TRUE,nrow=2) a[jj] ## [1] 4445 5556
source share