Roy Mendelssohn - NOAA Federal
2017-Jun-01 16:51 UTC
[R] Reversing one dimension of an array, in a generalized case
Hi All: I have been looking for an elegant way to do the following, but haven't found it, I have never had a good understanding of any of the "apply" functions. A simplified idea is I have an array, say: junk(5, 10, 3) where (5, 10, 3) give the dimension sizes, and I want to reverse the second dimension, so I could do: junk1 <- junk[, rev(seq_len(10), ] but what I am after is a general function that will do that where the array could be two, three or four dimensions, and I pass to the function which dimension I want to reverse, that is the function can not assume the number of dimensions of the array nor which dimension to reverse. For example, if i try: junk1 <- apply(junk, 2, rev) junk1 comes out as two-dimensional, not three-dimensional. It is probably something obvious but I am not getting it. Thanks for any help. -Roy ********************** "The contents of this message do not reflect any position of the U.S. Government or NOAA." ********************** Roy Mendelssohn Supervisory Operations Research Analyst NOAA/NMFS Environmental Research Division Southwest Fisheries Science Center ***Note new street address*** 110 McAllister Way Santa Cruz, CA 95060 Phone: (831)-420-3666 Fax: (831) 420-3980 e-mail: Roy.Mendelssohn at noaa.gov www: http://www.pfeg.noaa.gov/ "Old age and treachery will overcome youth and skill." "From those who have been given much, much will be expected" "the arc of the moral universe is long, but it bends toward justice" -MLK Jr.
Bert Gunter
2017-Jun-01 17:45 UTC
[R] Reversing one dimension of an array, in a generalized case
How about this: f <- function(a,wh){ ## a is the array; wh is the index to be reversed l<- lapply(dim(a),seq_len) l[[wh]]<- rev(l[[wh]]) do.call(`[`,c(list(a),l)) } ## test z <- array(1:120,dim=2:5) ## I omit the printouts f(z,2) f(z,3) Cheers, Bert Bert Gunter "The trouble with having an open mind is that people keep coming along and sticking things into it." -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) On Thu, Jun 1, 2017 at 9:51 AM, Roy Mendelssohn - NOAA Federal <roy.mendelssohn at noaa.gov> wrote:> Hi All: > > I have been looking for an elegant way to do the following, but haven't found it, I have never had a good understanding of any of the "apply" functions. > > A simplified idea is I have an array, say: > > junk(5, 10, 3) > > where (5, 10, 3) give the dimension sizes, and I want to reverse the second dimension, so I could do: > > junk1 <- junk[, rev(seq_len(10), ] > > but what I am after is a general function that will do that where the array could be two, three or four dimensions, and I pass to the function which dimension I want to reverse, that is the function can not assume the number of dimensions of the array nor which dimension to reverse. > > For example, if i try: > > junk1 <- apply(junk, 2, rev) > > junk1 comes out as two-dimensional, not three-dimensional. > > It is probably something obvious but I am not getting it. > > Thanks for any help. > > -Roy > > > ********************** > "The contents of this message do not reflect any position of the U.S. Government or NOAA." > ********************** > Roy Mendelssohn > Supervisory Operations Research Analyst > NOAA/NMFS > Environmental Research Division > Southwest Fisheries Science Center > ***Note new street address*** > 110 McAllister Way > Santa Cruz, CA 95060 > Phone: (831)-420-3666 > Fax: (831) 420-3980 > e-mail: Roy.Mendelssohn at noaa.gov www: http://www.pfeg.noaa.gov/ > > "Old age and treachery will overcome youth and skill." > "From those who have been given much, much will be expected" > "the arc of the moral universe is long, but it bends toward justice" -MLK Jr. > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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.
Roy Mendelssohn - NOAA Federal
2017-Jun-01 17:50 UTC
[R] Reversing one dimension of an array, in a generalized case
Thank you very much. I have a little test example I have been working with, and it does seem to work. I will have to go through and parse this to understand what you are doing What I had been doing is building up a string with the arguments and calling it, it works but very kludgy and very fragile. Thanks again. -Roy> On Jun 1, 2017, at 10:45 AM, Bert Gunter <bgunter.4567 at gmail.com> wrote: > > How about this: > > f <- function(a,wh){ ## a is the array; wh is the index to be reversed > l<- lapply(dim(a),seq_len) > l[[wh]]<- rev(l[[wh]]) > do.call(`[`,c(list(a),l)) > } > > ## test > z <- array(1:120,dim=2:5) > > ## I omit the printouts > > f(z,2) > > f(z,3) > > > Cheers, > Bert > > Bert Gunter > > "The trouble with having an open mind is that people keep coming along > and sticking things into it." > -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) > > > On Thu, Jun 1, 2017 at 9:51 AM, Roy Mendelssohn - NOAA Federal > <roy.mendelssohn at noaa.gov> wrote: >> Hi All: >> >> I have been looking for an elegant way to do the following, but haven't found it, I have never had a good understanding of any of the "apply" functions. >> >> A simplified idea is I have an array, say: >> >> junk(5, 10, 3) >> >> where (5, 10, 3) give the dimension sizes, and I want to reverse the second dimension, so I could do: >> >> junk1 <- junk[, rev(seq_len(10), ] >> >> but what I am after is a general function that will do that where the array could be two, three or four dimensions, and I pass to the function which dimension I want to reverse, that is the function can not assume the number of dimensions of the array nor which dimension to reverse. >> >> For example, if i try: >> >> junk1 <- apply(junk, 2, rev) >> >> junk1 comes out as two-dimensional, not three-dimensional. >> >> It is probably something obvious but I am not getting it. >> >> Thanks for any help. >> >> -Roy >> >> >> ********************** >> "The contents of this message do not reflect any position of the U.S. Government or NOAA." >> ********************** >> Roy Mendelssohn >> Supervisory Operations Research Analyst >> NOAA/NMFS >> Environmental Research Division >> Southwest Fisheries Science Center >> ***Note new street address*** >> 110 McAllister Way >> Santa Cruz, CA 95060 >> Phone: (831)-420-3666 >> Fax: (831) 420-3980 >> e-mail: Roy.Mendelssohn at noaa.gov www: http://www.pfeg.noaa.gov/ >> >> "Old age and treachery will overcome youth and skill." >> "From those who have been given much, much will be expected" >> "the arc of the moral universe is long, but it bends toward justice" -MLK Jr. >> >> ______________________________________________ >> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >> 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.********************** "The contents of this message do not reflect any position of the U.S. Government or NOAA." ********************** Roy Mendelssohn Supervisory Operations Research Analyst NOAA/NMFS Environmental Research Division Southwest Fisheries Science Center ***Note new street address*** 110 McAllister Way Santa Cruz, CA 95060 Phone: (831)-420-3666 Fax: (831) 420-3980 e-mail: Roy.Mendelssohn at noaa.gov www: http://www.pfeg.noaa.gov/ "Old age and treachery will overcome youth and skill." "From those who have been given much, much will be expected" "the arc of the moral universe is long, but it bends toward justice" -MLK Jr.
Jeff Newmiller
2017-Jun-01 18:31 UTC
[R] Reversing one dimension of an array, in a generalized case
Not sure it is "obvious", but this function implements what you describe: revdim <- function( a, d ) { dims <- attr( a, "dim" ) idxs <- lapply( seq_along( dims ) , function( dd ) { if ( d == dd ) seq.int( dims[ dd ], 1, -1 ) else seq.int( dims[ dd ] ) } ) do.call( `[`, c( list( a ), idxs ) ) } revdim( junk1, 2 ) On Thu, 1 Jun 2017, Roy Mendelssohn - NOAA Federal wrote:> Hi All: > > I have been looking for an elegant way to do the following, but haven't > found it, I have never had a good understanding of any of the "apply" > functions. > > A simplified idea is I have an array, say: > > junk(5, 10, 3) > > where (5, 10, 3) give the dimension sizes, and I want to reverse the > second dimension, so I could do: > > junk1 <- junk[, rev(seq_len(10), ] > > but what I am after is a general function that will do that where the > array could be two, three or four dimensions, and I pass to the function > which dimension I want to reverse, that is the function can not assume > the number of dimensions of the array nor which dimension to reverse. > > For example, if i try: > > junk1 <- apply(junk, 2, rev) > > junk1 comes out as two-dimensional, not three-dimensional. > > It is probably something obvious but I am not getting it. > > Thanks for any help. > > -Roy > >--------------------------------------------------------------------------- Jeff Newmiller The ..... ..... Go Live... DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go... Live: OO#.. Dead: OO#.. Playing Research Engineer (Solar/Batteries O.O#. #.O#. with /Software/Embedded Controllers) .OO#. .OO#. rocks...1k
David L Carlson
2017-Jun-01 18:34 UTC
[R] Reversing one dimension of an array, in a generalized case
Here is an alternative approach using apply(). Note that with apply() you are reversing rows or columns not indices of rows or columns so apply(junk, 2, rev) reverses the values in each column not the column indices. We actually need to use rev() on everything but the index we are interested in reversing: f2 <- function(a, wh) { dims <- seq_len(length(dim(a))) dims <- setdiff(dims, wh) apply(apply(a, dims, rev), dims, t) } # Your example j1 <- junk[ , rev(1:10), ] j2 <- f2(junk, 2) all.equal(j1, j2) # [1] TRUE # Bert's example z1 <- f(z, 2) z2 <- f2(z, 2) all.equal(z1, z2) # [1] TRUE ------------------------------------- David L Carlson Department of Anthropology Texas A&M University College Station, TX 77840-4352 -----Original Message----- From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Bert Gunter Sent: Thursday, June 1, 2017 12:46 PM To: Roy Mendelssohn - NOAA Federal <roy.mendelssohn at noaa.gov> Cc: R-help <r-help at r-project.org> Subject: Re: [R] Reversing one dimension of an array, in a generalized case How about this: f <- function(a,wh){ ## a is the array; wh is the index to be reversed l<- lapply(dim(a),seq_len) l[[wh]]<- rev(l[[wh]]) do.call(`[`,c(list(a),l)) } ## test z <- array(1:120,dim=2:5) ## I omit the printouts f(z,2) f(z,3) Cheers, Bert Bert Gunter "The trouble with having an open mind is that people keep coming along and sticking things into it." -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) On Thu, Jun 1, 2017 at 9:51 AM, Roy Mendelssohn - NOAA Federal <roy.mendelssohn at noaa.gov> wrote:> Hi All: > > I have been looking for an elegant way to do the following, but haven't found it, I have never had a good understanding of any of the "apply" functions. > > A simplified idea is I have an array, say: > > junk(5, 10, 3) > > where (5, 10, 3) give the dimension sizes, and I want to reverse the second dimension, so I could do: > > junk1 <- junk[, rev(seq_len(10), ] > > but what I am after is a general function that will do that where the array could be two, three or four dimensions, and I pass to the function which dimension I want to reverse, that is the function can not assume the number of dimensions of the array nor which dimension to reverse. > > For example, if i try: > > junk1 <- apply(junk, 2, rev) > > junk1 comes out as two-dimensional, not three-dimensional. > > It is probably something obvious but I am not getting it. > > Thanks for any help. > > -Roy > > > ********************** > "The contents of this message do not reflect any position of the U.S. Government or NOAA." > ********************** > Roy Mendelssohn > Supervisory Operations Research Analyst > NOAA/NMFS > Environmental Research Division > Southwest Fisheries Science Center > ***Note new street address*** > 110 McAllister Way > Santa Cruz, CA 95060 > Phone: (831)-420-3666 > Fax: (831) 420-3980 > e-mail: Roy.Mendelssohn at noaa.gov www: http://www.pfeg.noaa.gov/ > > "Old age and treachery will overcome youth and skill." > "From those who have been given much, much will be expected" > "the arc of the moral universe is long, but it bends toward justice" -MLK Jr. > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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.______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.
David Winsemius
2017-Jun-02 01:51 UTC
[R] Reversing one dimension of an array, in a generalized case
> On Jun 1, 2017, at 9:51 AM, Roy Mendelssohn - NOAA Federal <roy.mendelssohn at noaa.gov> wrote: > > Hi All: > > I have been looking for an elegant way to do the following, but haven't found it, I have never had a good understanding of any of the "apply" functions. > > A simplified idea is I have an array, say: > > junk(5, 10, 3) > > where (5, 10, 3) give the dimension sizes, and I want to reverse the second dimension, so I could do: > > junk1 <- junk[, rev(seq_len(10), ] > > but what I am after is a general function that will do that where the array could be two, three or four dimensions, and I pass to the function which dimension I want to reverse, that is the function can not assume the number of dimensions of the array nor which dimension to reverse. > > For example, if i try: > > junk1 <- apply(junk, 2, rev) > > junk1 comes out as two-dimensional, not three-dimensional. > > It is probably something obvious but I am not getting it.It was clear whether you wanted every slice along a particular dimension reversed or just one particular slice in a particular dimension. I thought probably the former: Try: (arr <- array(1:(2*3*4), dim=c(2,3,4) ) ) array( apply(arr, 3, rev), dim (arr) ) -- David.> > Thanks for any help. > > -Roy > > > ********************** > "The contents of this message do not reflect any position of the U.S. Government or NOAA." > ********************** > Roy Mendelssohn > Supervisory Operations Research Analyst > NOAA/NMFS > Environmental Research Division > Southwest Fisheries Science Center > ***Note new street address*** > 110 McAllister Way > Santa Cruz, CA 95060 > Phone: (831)-420-3666 > Fax: (831) 420-3980 > e-mail: Roy.Mendelssohn at noaa.gov www: http://www.pfeg.noaa.gov/ > > "Old age and treachery will overcome youth and skill." > "From those who have been given much, much will be expected" > "the arc of the moral universe is long, but it bends toward justice" -MLK Jr. > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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.David Winsemius Alameda, CA, USA
Seemingly Similar Threads
- Reversing one dimension of an array, in a generalized case
- Reversing one dimension of an array, in a generalized case
- Reversing one dimension of an array, in a generalized case
- Reversing one dimension of an array, in a generalized case
- Reversing one dimension of an array, in a generalized case