[R] array slice notation?

Bengoechea Bartolomé Enrique (SIES 73) enrique.bengoechea at credit-suisse.com
Thu Aug 6 09:55:32 CEST 2009


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:
> 
> You can do 
>> A <- HairEyeColor
>> do.call("[", c(list(A),list(1,T,T)))
>        Sex
> Eye     Male Female
>   Brown   32     36
>   Blue    11      9
>   Hazel   10      5
>   Green    3      2
> 
> Regards
> S?ren
> 

-- 
View this message in context: http://www.nabble.com/array-slice-notation--tp24814643p24819883.html
Sent from the R help mailing list archive at Nabble.com.




More information about the R-help mailing list