Re: [R] Subsetting an array by a vector of dimensions

From: Patrick Burns <pburns_at_pburns.seanet.com>
Date: Fri, 11 Jul 2008 16:51:44 +0100

I think you are looking for subscripting with a matrix:

x[cbind(1,1,2)]

See, for instance, the subscripting section of chapter 1 of S Poetry.

Patrick Burns
patrick_at_burns-stat.com
+44 (0)20 8525 0696
http://www.burns-stat.com
(home of S Poetry and "A Guide for the Unwilling S User")

Richard Pearson wrote:
> Hi
>
> Is it possible to subset an n-dimensional array by a vector of n
> dimensions? E.g. assume I have
>
>> x <- array(1:24, dim=2:4)
>> x[1,1,2]
> [1] 7
>> dims <- c(1,1,2)
>
> I would like a function that I can supply x and dims as parameters to,
> and have it return 7. Also, I would like to do something like:
>
>> x[1,1,]
> [1] 1 7 13 19
>> dims2<- c(1,1,NA)
>
> And have a function of x and dims2 that gives me back [1] 1 7 13 19
>
> Does such a thing exist?
>
> Thanks
>
> Richard
>
>



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. Received on Fri 11 Jul 2008 - 16:31:59 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Fri 11 Jul 2008 - 17:31:42 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive