[R] operating on arrays of unknown dimensionality

Vadim Organovich vogranovich at jumptrading.com
Fri Aug 8 18:42:11 CEST 2008


Dear R-users,

I am looking for a way to assign to slices of arrays where dimensionality of the array is not a-priory known.

Specifically, I would like to be able to generalize the following example of dimensionality 2 to an arbitrary diminsionality:

In this example we create an array x, a smaller array y and then assign y to a slice of x.

> dimnmx <- list(c('a','b'), c('x','y','z')); x <- array(0, dim=sapply(dimnmx, length), dimnames=dimnmx)
> x
  x y z
a 0 0 0
b 0 0 0
> dimnmy <- list(c('a','b'), c('x','z')); y <- array(1, dim=sapply(dimnmy, length), dimnames=dimnmy)
> y
  x z
a 1 1
b 1 1
> x[dimnames(y)[[1]], dimnames(y)[[2]]] <- y
> x
  x y z
a 1 0 1
b 1 0 1


Now suppose I have an arbitrary lists dimnmx and dimnmy which are dimnames of respective arrays y and x. How do I assign y to the slice of x sliced out by dimnmy:

> dimnmx <- list(c('a','b'), c('x','y','z'), c('1','2')); x <- array(0, dim=sapply(dimnmx, length), dimnames=dimnmx)
> dimnmy <- list(c('a','b'), c('x','z'), c('2')); y <- array(1, dim=sapply(dimnmy, length), dimnames=dimnmy)
> something to the effect of do.call('[', list(x, dimnames(y))) <- y

Thanks,
Vadim

Note: This email is for the confidential use of the named addressee(s) only and may contain proprietary, confidential or privileged information. If you are not the intended recipient, you are hereby notified that any review, dissemination or copying of this email is strictly prohibited, and to please notify the sender immediately and destroy this email and any attachments.  Email transmission cannot be guaranteed to be secure or error-free.  Jump Trading, therefore, does not make any guarantees as to the completeness or accuracy of this email or any attachments.  This email is for informational purposes only and does not constitute a recommendation, offer, request or solicitation of any kind to buy, sell, subscribe, redeem or perform any type of transaction of a financial product.



More information about the R-help mailing list