On Fri, 8 Aug 2008, Vadim Organovich wrote:
> 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
I think this is the idiom you want:
do.call( `[<-` , c( list(x), dimnames(y), list( y ) ) )
and if you want to save this to `x`
x <- do.call(`[<-`, c(list(x), dimnames(y), list( y ) ) )
HTH,
Chuck
>
> 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.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
Charles C. Berry (858) 534-2098
Dept of Family/Preventive Medicine
E mailto:cberry at tajo.ucsd.edu UC San Diego
http://famprevmed.ucsd.edu/faculty/cberry/ La Jolla, San Diego 92093-0901