Ismail SEZEN
2017-Jun-01 20:35 UTC
[R] Reversing one dimension of an array, in a generalized case
> On 1 Jun 2017, at 22:42, Roy Mendelssohn - NOAA Federal <roy.mendelssohn at noaa.gov> wrote: > > Thanks to all for responses/. There was a question of exactly what was wanted. It is the generalization of the obvious example I gave, > >>>> junk1 <- junk[, rev(seq_len(10), ] > > > so that > > junk[1,1,1 ] = junk1[1,10,1] > junk[1,2,1] = junk1[1,9,1] > > etc. > > The genesis of this is the program is downloading data from a variety of sources on (time, altitude, lat, lon) coordinates, but all the coordinates are not always there, and sometime the latitude coordinates go from north to south and sometimes from south to north. I want to always return the data going from south to north, so if I find that the data is north to south, I have to first reverse the array with the coordinate values (easy enough), and then reverse the one dimension in the data array that corresponds to latitude. The downloaded information tells me which dimension is latitude plus how many coordinates are in the data.Hello Roy, Perhaps you are aware of but I want to mention anyway. Basic issue is that you always want latitudes are monotonously increasing. Let me tell what I do when I read a ncdf file: 1- Set latitudes always monotonously decreasing (from 90 to -90) 2- Set longitudes always mononously increasing but from -180 to 180. 3- Set levels always monotonously decreasing (this is not relevant) Why? If you plan to plot variables in R, you will need coordinates in this order. For instance, if you set latitudes monotonously increasing, your map will be plotted upside down. To fix this, you will need reverse dimension again. And also if your longitudes ranges from 0 to 360, you will see the only the east side of the plot on a world map. West of Greencwich will be empty. They were the problems that I faced last year when I tried to plot netcdf files using lattice and rasterVis packages.> > As I the said, I haven't done extensive testing on what Bert sent, but on a toy 3-dimensional example I have it appeared to do what I want. > > Thanks again, > > -Roy > >> On Jun 1, 2017, at 12:22 PM, David L Carlson <dcarlson at tamu.edu> wrote: >> >> My error. Clearly I did not do enough testing. >> >> z <- array(1:24,dim=2:4) >>> all.equal(f(z,1),f2(z,1)) >> [1] TRUE >>> all.equal(f(z,2),f2(z,2)) >> [1] TRUE >>> all.equal(f(z,3),f2(z,3)) >> [1] "Attributes: < Component ?dim?: Mean relative difference: 0.4444444 >" >> [2] "Mean relative difference: 0.6109091" >> >> # Your earlier example >>> z <- array(1:120, dim=2:5) >>> all.equal(f(z,1),f2(z,1)) >> [1] TRUE >>> all.equal(f(z,2),f2(z,2)) >> [1] TRUE >>> all.equal(f(z,3),f2(z,3)) >> [1] "Attributes: < Component ?dim?: Mean relative difference: 0.4444444 >" >> [2] "Mean relative difference: 0.1262209" >>> all.equal(f(z,4),f2(z,4)) >> [1] "Attributes: < Component ?dim?: Mean relative difference: 0.5714286 >" >> [2] "Mean relative difference: 0.5855162" >> >> David C >> >> -----Original Message----- >> From: Bert Gunter [mailto:bgunter.4567 at gmail.com] >> Sent: Thursday, June 1, 2017 2:00 PM >> To: David L Carlson <dcarlson at tamu.edu> >> Cc: Roy Mendelssohn - NOAA Federal <roy.mendelssohn at noaa.gov>; R-help <r-help at r-project.org> >> Subject: Re: [R] Reversing one dimension of an array, in a generalized case >> >> ?? >> >>> z <- array(1:24,dim=2:4) >>> all.equal(f(z,3),f2(z,3)) >> >> [1] "Attributes: < Component ?dim?: Mean relative difference: 0.4444444 >" >> [2] "Mean relative difference: 0.6109091" >> >> In fact, >> >>> dim(f(z,3)) >> [1] 2 3 4 >> >>> dim(f2(z,3)) >> [1] 3 4 2 >> >> Have I made some sort of stupid error here? Or have I misunderstood >> what was wanted? >> >> 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 11:34 AM, David L Carlson <dcarlson at tamu.edu> wrote: >>> 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. > > ********************** > "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 <mailto:Roy.Mendelssohn at noaa.gov> www: http://www.pfeg.noaa.gov/ <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 <mailto:R-help at r-project.org> mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help <https://stat.ethz.ch/mailman/listinfo/r-help> > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html <http://www.r-project.org/posting-guide.html> > and provide commented, minimal, self-contained, reproducible code.[[alternative HTML version deleted]]
Roy Mendelssohn - NOAA Federal
2017-Jun-01 20:43 UTC
[R] Reversing one dimension of an array, in a generalized case
Thank you. That ignores certain standards in the communities I work in, it also ignore the fact that whether I decide to always return increasing or decreasing latitudes, if the sources aren't consistent, then I need to reverse some of the data, no matter which way I decide. Increasing latitudes (and longitudes) among other things have the nice property that the array indices are also increasing in the same way. I do a lot of mapping in R, and a number of the mapping routines actually require you to "melt" the data to long-form in which case the order of the latitudes in the array is irrelevant, as long as the mapping is correct. -Roy> On Jun 1, 2017, at 1:35 PM, Ismail SEZEN <sezenismail at gmail.com> wrote: > >> >> On 1 Jun 2017, at 22:42, Roy Mendelssohn - NOAA Federal <roy.mendelssohn at noaa.gov> wrote: >> >> Thanks to all for responses/. There was a question of exactly what was wanted. It is the generalization of the obvious example I gave, >> >>>>> junk1 <- junk[, rev(seq_len(10), ] >> >> >> so that >> >> junk[1,1,1 ] = junk1[1,10,1] >> junk[1,2,1] = junk1[1,9,1] >> >> etc. >> >> The genesis of this is the program is downloading data from a variety of sources on (time, altitude, lat, lon) coordinates, but all the coordinates are not always there, and sometime the latitude coordinates go from north to south and sometimes from south to north. I want to always return the data going from south to north, so if I find that the data is north to south, I have to first reverse the array with the coordinate values (easy enough), and then reverse the one dimension in the data array that corresponds to latitude. The downloaded information tells me which dimension is latitude plus how many coordinates are in the data. > > Hello Roy, > Perhaps you are aware of but I want to mention anyway. Basic issue is that you always want latitudes are monotonously increasing. Let me tell what I do when I read a ncdf file: > > 1- Set latitudes always monotonously decreasing (from 90 to -90) > 2- Set longitudes always mononously increasing but from -180 to 180. > 3- Set levels always monotonously decreasing (this is not relevant) > > Why? If you plan to plot variables in R, you will need coordinates in this order. For instance, if you set latitudes monotonously increasing, your map will be plotted upside down. To fix this, you will need reverse dimension again. And also if your longitudes ranges from 0 to 360, you will see the only the east side of the plot on a world map. West of Greencwich will be empty. They were the problems that I faced last year when I tried to plot netcdf files using lattice and rasterVis packages. > > >> >> As I the said, I haven't done extensive testing on what Bert sent, but on a toy 3-dimensional example I have it appeared to do what I want. >> >> Thanks again, >> >> -Roy >> >>> On Jun 1, 2017, at 12:22 PM, David L Carlson <dcarlson at tamu.edu> wrote: >>> >>> My error. Clearly I did not do enough testing. >>> >>> z <- array(1:24,dim=2:4) >>>> all.equal(f(z,1),f2(z,1)) >>> [1] TRUE >>>> all.equal(f(z,2),f2(z,2)) >>> [1] TRUE >>>> all.equal(f(z,3),f2(z,3)) >>> [1] "Attributes: < Component ?dim?: Mean relative difference: 0.4444444 >" >>> [2] "Mean relative difference: 0.6109091" >>> >>> # Your earlier example >>>> z <- array(1:120, dim=2:5) >>>> all.equal(f(z,1),f2(z,1)) >>> [1] TRUE >>>> all.equal(f(z,2),f2(z,2)) >>> [1] TRUE >>>> all.equal(f(z,3),f2(z,3)) >>> [1] "Attributes: < Component ?dim?: Mean relative difference: 0.4444444 >" >>> [2] "Mean relative difference: 0.1262209" >>>> all.equal(f(z,4),f2(z,4)) >>> [1] "Attributes: < Component ?dim?: Mean relative difference: 0.5714286 >" >>> [2] "Mean relative difference: 0.5855162" >>> >>> David C >>> >>> -----Original Message----- >>> From: Bert Gunter [mailto:bgunter.4567 at gmail.com] >>> Sent: Thursday, June 1, 2017 2:00 PM >>> To: David L Carlson <dcarlson at tamu.edu> >>> Cc: Roy Mendelssohn - NOAA Federal <roy.mendelssohn at noaa.gov>; R-help <r-help at r-project.org> >>> Subject: Re: [R] Reversing one dimension of an array, in a generalized case >>> >>> ?? >>> >>>> z <- array(1:24,dim=2:4) >>>> all.equal(f(z,3),f2(z,3)) >>> >>> [1] "Attributes: < Component ?dim?: Mean relative difference: 0.4444444 >" >>> [2] "Mean relative difference: 0.6109091" >>> >>> In fact, >>> >>>> dim(f(z,3)) >>> [1] 2 3 4 >>> >>>> dim(f2(z,3)) >>> [1] 3 4 2 >>> >>> Have I made some sort of stupid error here? Or have I misunderstood >>> what was wanted? >>> >>> 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 11:34 AM, David L Carlson <dcarlson at tamu.edu> wrote: >>>> 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. >> >> ********************** >> "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.
David L Carlson
2017-Jun-01 20:56 UTC
[R] Reversing one dimension of an array, in a generalized case
On the off chance that anyone is still interested, here is the corrected function using aperm(): z <- array(1:120,dim=2:5) f2 <- function(a, wh) { idx <- seq_len(length(dim(a))) dims <- setdiff(idx, wh) idx <- append(idx[-1], idx[1], wh-1) aperm(apply(a, dims, rev), idx) } all.equal(f(z, 1), f2(z, 1)) # [1] TRUE all.equal(f(z, 2), f2(z, 2)) # [1] TRUE all.equal(f(z, 3), f2(z, 3)) # [1] TRUE all.equal(f(z, 4), f2(z, 4)) # [1] TRUE David C From: Ismail SEZEN [mailto:sezenismail at gmail.com] Sent: Thursday, June 1, 2017 3:35 PM To: Roy Mendelssohn - NOAA Federal <roy.mendelssohn at noaa.gov> Cc: David L Carlson <dcarlson at tamu.edu>; R-help <r-help at r-project.org> Subject: Re: [R] Reversing one dimension of an array, in a generalized case On 1 Jun 2017, at 22:42, Roy Mendelssohn - NOAA Federal <roy.mendelssohn at noaa.gov> wrote: Thanks to all for responses/. ?There was a question of exactly what was wanted. ?It is the generalization of the obvious example I gave, ? junk1 <- junk[, rev(seq_len(10), ] so that junk[1,1,1 ] = junk1[1,10,1] junk[1,2,1] ?= junk1[1,9,1] etc. The genesis of this is the program is downloading data from a variety of sources on (time, altitude, lat, lon) coordinates, ?but all the coordinates are not always there, and sometime the latitude coordinates go from north to south and sometimes from south to north. ?I want to always return the data going from south to north, so if I find that the data is north to south, ?I have to first reverse the array with the coordinate values (easy enough), ?and then reverse the one dimension in the data array that corresponds to latitude. The downloaded information tells me which dimension is latitude plus how many coordinates are in the data. Hello Roy, Perhaps you are aware of but I want to mention anyway. Basic issue is that you always want latitudes are monotonously increasing. Let me tell what I do when I read a ncdf file: 1- Set latitudes always monotonously decreasing (from 90 to -90) 2- Set longitudes always mononously increasing but from -180 to 180. 3- Set levels always monotonously decreasing (this is not relevant) Why? If you plan to plot variables in R, you will need coordinates in this order. For instance, if you set latitudes monotonously increasing, your map will be plotted upside down. To fix this, you will need reverse dimension again. And also if your longitudes ranges from 0 to 360, you will see the only the east side of the plot on a world map. West of Greencwich will be empty. ?They were the problems that I faced last year when I tried to plot netcdf files using lattice and rasterVis packages.? As I the said, ?I haven't done extensive testing on what Bert sent, ?but on a toy 3-dimensional example I have it appeared to do what I want. Thanks again, -Roy On Jun 1, 2017, at 12:22 PM, David L Carlson <dcarlson at tamu.edu> wrote: My error. Clearly I did not do enough testing. z <- array(1:24,dim=2:4) all.equal(f(z,1),f2(z,1)) [1] TRUE all.equal(f(z,2),f2(z,2)) [1] TRUE all.equal(f(z,3),f2(z,3)) [1] "Attributes: < Component ?dim?: Mean relative difference: 0.4444444 >" [2] "Mean relative difference: 0.6109091" ??? # Your earlier example z <- array(1:120, dim=2:5) all.equal(f(z,1),f2(z,1)) [1] TRUE all.equal(f(z,2),f2(z,2)) [1] TRUE all.equal(f(z,3),f2(z,3)) [1] "Attributes: < Component ?dim?: Mean relative difference: 0.4444444 >" [2] "Mean relative difference: 0.1262209" ???????????????????????????????? all.equal(f(z,4),f2(z,4)) [1] "Attributes: < Component ?dim?: Mean relative difference: 0.5714286 >" [2] "Mean relative difference: 0.5855162" ? David C -----Original Message----- From: Bert Gunter [mailto:bgunter.4567 at gmail.com]? Sent: Thursday, June 1, 2017 2:00 PM To: David L Carlson <dcarlson at tamu.edu> Cc: Roy Mendelssohn - NOAA Federal <roy.mendelssohn at noaa.gov>; R-help <r-help at r-project.org> Subject: Re: [R] Reversing one dimension of an array, in a generalized case ?? z <- array(1:24,dim=2:4) all.equal(f(z,3),f2(z,3)) [1] "Attributes: < Component ?dim?: Mean relative difference: 0.4444444 >" [2] "Mean relative difference: 0.6109091" In fact, dim(f(z,3)) [1] 2 3 4 dim(f2(z,3)) [1] 3 4 2 Have I made some sort of stupid error here? Or have I misunderstood what was wanted? 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 11:34 AM, David L Carlson <dcarlson at tamu.edu> wrote: 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. ********************** "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 20:59 UTC
[R] Reversing one dimension of an array, in a generalized case
Thanks again. I am going to try the different versions. But I probably won't be able to get to it till next week. This is probably at the point where anything further should be sent to me privately. -Roy> On Jun 1, 2017, at 1:56 PM, David L Carlson <dcarlson at tamu.edu> wrote: > > On the off chance that anyone is still interested, here is the corrected function using aperm(): > > z <- array(1:120,dim=2:5) > f2 <- function(a, wh) { > idx <- seq_len(length(dim(a))) > dims <- setdiff(idx, wh) > idx <- append(idx[-1], idx[1], wh-1) > aperm(apply(a, dims, rev), idx) > } > > all.equal(f(z, 1), f2(z, 1)) > # [1] TRUE > all.equal(f(z, 2), f2(z, 2)) > # [1] TRUE > all.equal(f(z, 3), f2(z, 3)) > # [1] TRUE > all.equal(f(z, 4), f2(z, 4)) > # [1] TRUE > > David C > > > From: Ismail SEZEN [mailto:sezenismail at gmail.com] > Sent: Thursday, June 1, 2017 3:35 PM > To: Roy Mendelssohn - NOAA Federal <roy.mendelssohn at noaa.gov> > Cc: David L Carlson <dcarlson at tamu.edu>; R-help <r-help at r-project.org> > Subject: Re: [R] Reversing one dimension of an array, in a generalized case > > > On 1 Jun 2017, at 22:42, Roy Mendelssohn - NOAA Federal <roy.mendelssohn at noaa.gov> wrote: > > Thanks to all for responses/. There was a question of exactly what was wanted. It is the generalization of the obvious example I gave, > > > junk1 <- junk[, rev(seq_len(10), ] > > > so that > > junk[1,1,1 ] = junk1[1,10,1] > junk[1,2,1] = junk1[1,9,1] > > etc. > > The genesis of this is the program is downloading data from a variety of sources on (time, altitude, lat, lon) coordinates, but all the coordinates are not always there, and sometime the latitude coordinates go from north to south and sometimes from south to north. I want to always return the data going from south to north, so if I find that the data is north to south, I have to first reverse the array with the coordinate values (easy enough), and then reverse the one dimension in the data array that corresponds to latitude. The downloaded information tells me which dimension is latitude plus how many coordinates are in the data. > > Hello Roy, > Perhaps you are aware of but I want to mention anyway. Basic issue is that you always want latitudes are monotonously increasing. Let me tell what I do when I read a ncdf file: > > 1- Set latitudes always monotonously decreasing (from 90 to -90) > 2- Set longitudes always mononously increasing but from -180 to 180. > 3- Set levels always monotonously decreasing (this is not relevant) > > Why? If you plan to plot variables in R, you will need coordinates in this order. For instance, if you set latitudes monotonously increasing, your map will be plotted upside down. To fix this, you will need reverse dimension again. And also if your longitudes ranges from 0 to 360, you will see the only the east side of the plot on a world map. West of Greencwich will be empty. They were the problems that I faced last year when I tried to plot netcdf files using lattice and rasterVis packages. > > > > > As I the said, I haven't done extensive testing on what Bert sent, but on a toy 3-dimensional example I have it appeared to do what I want. > > Thanks again, > > -Roy > > > On Jun 1, 2017, at 12:22 PM, David L Carlson <dcarlson at tamu.edu> wrote: > > My error. Clearly I did not do enough testing. > > z <- array(1:24,dim=2:4) > > all.equal(f(z,1),f2(z,1)) > [1] TRUE > > all.equal(f(z,2),f2(z,2)) > [1] TRUE > > all.equal(f(z,3),f2(z,3)) > [1] "Attributes: < Component ?dim?: Mean relative difference: 0.4444444 >" > [2] "Mean relative difference: 0.6109091" > > # Your earlier example > > z <- array(1:120, dim=2:5) > all.equal(f(z,1),f2(z,1)) > [1] TRUE > > all.equal(f(z,2),f2(z,2)) > [1] TRUE > > all.equal(f(z,3),f2(z,3)) > [1] "Attributes: < Component ?dim?: Mean relative difference: 0.4444444 >" > [2] "Mean relative difference: 0.1262209" > > all.equal(f(z,4),f2(z,4)) > [1] "Attributes: < Component ?dim?: Mean relative difference: 0.5714286 >" > [2] "Mean relative difference: 0.5855162" > > David C > > -----Original Message----- > From: Bert Gunter [mailto:bgunter.4567 at gmail.com] > Sent: Thursday, June 1, 2017 2:00 PM > To: David L Carlson <dcarlson at tamu.edu> > Cc: Roy Mendelssohn - NOAA Federal <roy.mendelssohn at noaa.gov>; R-help <r-help at r-project.org> > Subject: Re: [R] Reversing one dimension of an array, in a generalized case > > ?? > > > z <- array(1:24,dim=2:4) > all.equal(f(z,3),f2(z,3)) > > [1] "Attributes: < Component ?dim?: Mean relative difference: 0.4444444 >" > [2] "Mean relative difference: 0.6109091" > > In fact, > > > dim(f(z,3)) > [1] 2 3 4 > > > dim(f2(z,3)) > [1] 3 4 2 > > Have I made some sort of stupid error here? Or have I misunderstood > what was wanted? > > 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 11:34 AM, David L Carlson <dcarlson at tamu.edu> wrote: > > 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. > > ********************** > "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.
Reasonably Related 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