[R] building a subscript programatically

Comcast dwinsemius at comcast.net
Wed Nov 2 01:22:05 CET 2011


Leaving the indices empty should give you what I'm guessing you want/expect.

x[,,2]    #.  TRUE would also work, just not in a list.

David.

On Nov 1, 2011, at 6:14 PM, Ernest Adrogué <nfdisco at gmail.com> wrote:

> Hi,
> 
> On ocasion, you need to subscript an array that has an arbitrary
> (ie. not known in advance) number of dimensions. How do you deal with
> these situations?
> It appears that it is not possible use a list as an index, for
> instance this fails: 
> 
>> x <- array(NA, c(2,2,2))
>> x[list(TRUE,TRUE,2)]
> Error in x[list(TRUE, TRUE, 2)] : invalid subscript type 'list'
> 
> The only way I know is using do.call() but it's rather ugly. There
> must be a better way!!
> 
>> do.call('[', c(list(x), TRUE, TRUE, 2))
>     [,1] [,2]
> [1,]   NA   NA
> [2,]   NA   NA
> 
> Any idea?
> 
> Regards,
> Ernest
> 
> ______________________________________________
> 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.



More information about the R-help mailing list