[R] array slice notation?
Steve Lianoglou
mailinglist.honeypot at gmail.com
Tue Aug 4 21:35:22 CEST 2009
Hi,
On Aug 4, 2009, at 3:23 PM, Steve Jaffe wrote:
>
> Suppose I have an n-diml array A and I want to extract the first
> "row" -- ie
> all elements A[1, ...]
>
> Interactively if I know 'n' I can write A[1,,,,,] with (n-1) commas.
>
> How do I do the same more generally, eg in a script?
>
> (I can think of doing this by converting A to a vector then
> extracting the
> approp elements then reshaping it to an array, but I wonder if there
> isn't a
> more straightforward approach)
You actually don't have to convert A to a vector, you can use vector-
style indexing into a matrix:
R> m <- matrix(1:20, 4)
R> m
[,1] [,2] [,3] [,4] [,5]
[1,] 1 5 9 13 17
[2,] 2 6 10 14 18
[3,] 3 7 11 15 19
[4,] 4 8 12 16 20
R> m[,3]
[1] 9 10 11 12
R> m[9:12]
[1] 9 10 11 12
You're just left to calculate the correct (linear) indices, which I
guess isn't too (too) bad.
-steve
--
Steve Lianoglou
Graduate Student: Computational Systems Biology
| Memorial Sloan-Kettering Cancer Center
| Weill Medical College of Cornell University
Contact Info: http://cbio.mskcc.org/~lianos/contact
More information about the R-help
mailing list