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) Thanks -- View this message in context: http://www.nabble.com/array-slice-notation--tp24814643p24814643.html Sent from the R help mailing list archive at Nabble.com.
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
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 ________________________________________ Fra: r-help-bounces at r-project.org [r-help-bounces at r-project.org] På vegne af Steve Jaffe [sjaffe at riskspan.com] Sendt: 4. august 2009 21:23 Til: r-help at r-project.org Emne: [R] array slice notation? 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) Thanks -- View this message in context: http://www.nabble.com/array-slice-notation--tp24814643p24814643.html Sent from the R help mailing list archive at Nabble.com. ______________________________________________ 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.
Hi Steve, I had the same problem when writing the hierobarp function in plotrix. Have a look at the source code (v2.6-4 in my copy lines 128-133) as the solution seems to work well. Jim
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.