Skip to content

array slice notation?

1 message · Bengoechea Bartolomé Enrique (SIES 73)

#
Hi,

The do.call() version is likely slower than the vector version.

To select multiple rows, just replace the 1 by the vector with the rows.

The TRUEs in the list fail if the array happens to have 0 length in the corresponding dimension. You can build the list passed to do.call() first setting zeros instead of TRUE if that is the case:

A <- array(1:27, c(3, 3, 3))
v <- c(1, 2)
largs <- lapply(dim(A)[-1L], function(y) if (y) TRUE else 0L)
do.call("[", c(list(A, v), largs))


Enrique

------------------------------
Date: Tue, 4 Aug 2009 19:45:55 -0700 (PDT)
From: Steve Jaffe <sjaffe at riskspan.com>
Subject: Re: [R] array slice notation?
To: r-help at r-project.org
Message-ID: <24819883.post at talk.nabble.com>
Content-Type: text/plain; charset=UTF-8


Very nice. Two questions:
1> Do you have any idea of the timing difference, if any, between this and
the vector-subscripting method?
2> How do  you generalize this to select multiple rows eg with indexes given
by a vector 'v'?
S?ren H?jsgaard wrote: