Reversing one dimension of an array, in a generalized case

classic Classic list List threaded Threaded
15 messages Options
Reply | Threaded
Open this post in threaded view
|

Reversing one dimension of an array, in a generalized case

Roy Mendelssohn - NOAA Federal
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: [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Reversing one dimension of an array, in a generalized case

Bert Gunter-2
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
<[hidden email]> 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: [hidden email] 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.
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Reversing one dimension of an array, in a generalized case

Roy Mendelssohn - NOAA Federal
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 <[hidden email]> 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
> <[hidden email]> 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: [hidden email] 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.
>>
>> ______________________________________________
>> [hidden email] 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: [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Reversing one dimension of an array, in a generalized case

Jeff Newmiller
In reply to this post by Roy Mendelssohn - NOAA Federal
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:<[hidden email]>        Basics: ##.#.       ##.#.  Live Go...
                                       Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Reversing one dimension of an array, in a generalized case

David Carlson
In reply to this post by Bert Gunter-2
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:[hidden email]] On Behalf Of Bert Gunter
Sent: Thursday, June 1, 2017 12:46 PM
To: Roy Mendelssohn - NOAA Federal <[hidden email]>
Cc: R-help <[hidden email]>
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
<[hidden email]> 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: [hidden email] 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.
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Reversing one dimension of an array, in a generalized case

Bert Gunter-2
??

> 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 <[hidden email]> 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:[hidden email]] On Behalf Of Bert Gunter
> Sent: Thursday, June 1, 2017 12:46 PM
> To: Roy Mendelssohn - NOAA Federal <[hidden email]>
> Cc: R-help <[hidden email]>
> 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
> <[hidden email]> 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: [hidden email] 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.
>>
>> ______________________________________________
>> [hidden email] 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.
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Reversing one dimension of an array, in a generalized case

David Carlson
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:[hidden email]]
Sent: Thursday, June 1, 2017 2:00 PM
To: David L Carlson <[hidden email]>
Cc: Roy Mendelssohn - NOAA Federal <[hidden email]>; R-help <[hidden email]>
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 <[hidden email]> 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:[hidden email]] On Behalf Of Bert Gunter
> Sent: Thursday, June 1, 2017 12:46 PM
> To: Roy Mendelssohn - NOAA Federal <[hidden email]>
> Cc: R-help <[hidden email]>
> 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
> <[hidden email]> 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: [hidden email] 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.
>>
>> ______________________________________________
>> [hidden email] 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.
>
> ______________________________________________
> [hidden email] 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.
______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Reversing one dimension of an array, in a generalized case

Ismail SEZEN
In reply to this post by Bert Gunter-2
And my 2 cents,

Rev <- function(x, margin) {
    newdim <- rep("", length(dim(x)))
    newdim[margin] <- paste(dim(x), ":1", sep = "")[margin]
    z <- eval(parse(text = gettextf("x[%s,drop = F]", paste(newdim, sep = "",
                                                            collapse = ","))))
    class(z) <- oldClass(x)
    return(z)
}

z <- array(1:24,dim=2:4)
dim(f(z, 3)) # 2 3 4
dim(f2(z, 3)) # 3 4 2
dim(Rev(z, 3)) # 2 3 4
dim(revdim(z,3)) # 2 3 4

microbenchmark::microbenchmark(f(z, 3), Rev(z, 3), revdim(z,3), f2(z,3))

Unit: microseconds
         expr     min       lq      mean   median       uq     max neval
      f(z, 3)   6.356   7.6090   9.74268   9.3285  11.2325  35.571   100
    Rev(z, 3) 161.079 166.9660 175.26906 172.1450 176.8130 273.078   100
 revdim(z, 3)   5.011   6.1300   7.88565   7.5695   9.0500  21.301   100
     f2(z, 3)  68.454  71.6815  82.85703  81.6700  87.9285 126.496   100

and strangely,

all.equal(f(z, 3), Rev(z, 3), revdim(z,3), f2(z,3))
[1] TRUE

?

> On 1 Jun 2017, at 22:00, Bert Gunter <[hidden email]> wrote:
>
> ??
>
>> 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 <[hidden email]> 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:[hidden email]] On Behalf Of Bert Gunter
>> Sent: Thursday, June 1, 2017 12:46 PM
>> To: Roy Mendelssohn - NOAA Federal <[hidden email]>
>> Cc: R-help <[hidden email]>
>> 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
>> <[hidden email]> 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: [hidden email] 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.
>>>
>>> ______________________________________________
>>> [hidden email] 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.
>>
>> ______________________________________________
>> [hidden email] 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.
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Reversing one dimension of an array, in a generalized case

Roy Mendelssohn - NOAA Federal
In reply to this post by David Carlson
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.

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 <[hidden email]> 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:[hidden email]]
> Sent: Thursday, June 1, 2017 2:00 PM
> To: David L Carlson <[hidden email]>
> Cc: Roy Mendelssohn - NOAA Federal <[hidden email]>; R-help <[hidden email]>
> 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 <[hidden email]> 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:[hidden email]] On Behalf Of Bert Gunter
>> Sent: Thursday, June 1, 2017 12:46 PM
>> To: Roy Mendelssohn - NOAA Federal <[hidden email]>
>> Cc: R-help <[hidden email]>
>> 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
>> <[hidden email]> 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: [hidden email] 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.
>>>
>>> ______________________________________________
>>> [hidden email] 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.
>>
>> ______________________________________________
>> [hidden email] 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: [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Reversing one dimension of an array, in a generalized case

Ismail SEZEN

> On 1 Jun 2017, at 22:42, Roy Mendelssohn - NOAA Federal <[hidden email]> 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 <[hidden email]> 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:[hidden email]]
>> Sent: Thursday, June 1, 2017 2:00 PM
>> To: David L Carlson <[hidden email]>
>> Cc: Roy Mendelssohn - NOAA Federal <[hidden email]>; R-help <[hidden email]>
>> 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 <[hidden email]> 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:[hidden email]] On Behalf Of Bert Gunter
>>> Sent: Thursday, June 1, 2017 12:46 PM
>>> To: Roy Mendelssohn - NOAA Federal <[hidden email]>
>>> Cc: R-help <[hidden email]>
>>> 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
>>> <[hidden email]> 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: [hidden email] 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.
>>>>
>>>> ______________________________________________
>>>> [hidden email] 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.
>>>
>>> ______________________________________________
>>> [hidden email] 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: [hidden email] <mailto:[hidden email]> 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.
>
> ______________________________________________
> [hidden email] <mailto:[hidden email]> 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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Reversing one dimension of an array, in a generalized case

Roy Mendelssohn - NOAA Federal
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 <[hidden email]> wrote:
>
>>
>> On 1 Jun 2017, at 22:42, Roy Mendelssohn - NOAA Federal <[hidden email]> 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 <[hidden email]> 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:[hidden email]]
>>> Sent: Thursday, June 1, 2017 2:00 PM
>>> To: David L Carlson <[hidden email]>
>>> Cc: Roy Mendelssohn - NOAA Federal <[hidden email]>; R-help <[hidden email]>
>>> 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 <[hidden email]> 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:[hidden email]] On Behalf Of Bert Gunter
>>>> Sent: Thursday, June 1, 2017 12:46 PM
>>>> To: Roy Mendelssohn - NOAA Federal <[hidden email]>
>>>> Cc: R-help <[hidden email]>
>>>> 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
>>>> <[hidden email]> 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: [hidden email] 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.
>>>>>
>>>>> ______________________________________________
>>>>> [hidden email] 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.
>>>>
>>>> ______________________________________________
>>>> [hidden email] 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: [hidden email] 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.
>>
>> ______________________________________________
>> [hidden email] 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: [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Reversing one dimension of an array, in a generalized case

David Carlson
In reply to this post by Ismail SEZEN
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:[hidden email]]
Sent: Thursday, June 1, 2017 3:35 PM
To: Roy Mendelssohn - NOAA Federal <[hidden email]>
Cc: David L Carlson <[hidden email]>; R-help <[hidden email]>
Subject: Re: [R] Reversing one dimension of an array, in a generalized case


On 1 Jun 2017, at 22:42, Roy Mendelssohn - NOAA Federal <[hidden email]> 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 <[hidden email]> 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:[hidden email]
Sent: Thursday, June 1, 2017 2:00 PM
To: David L Carlson <[hidden email]>
Cc: Roy Mendelssohn - NOAA Federal <[hidden email]>; R-help <[hidden email]>
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 <[hidden email]> 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:[hidden email]] On Behalf Of Bert Gunter
Sent: Thursday, June 1, 2017 12:46 PM
To: Roy Mendelssohn - NOAA Federal <[hidden email]>
Cc: R-help <[hidden email]>
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
<[hidden email]> 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: [hidden email] 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.

______________________________________________
[hidden email] 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.

______________________________________________
[hidden email] 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: [hidden email] 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.

______________________________________________
[hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Reversing one dimension of an array, in a generalized case

Roy Mendelssohn - NOAA Federal
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 <[hidden email]> 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:[hidden email]]
> Sent: Thursday, June 1, 2017 3:35 PM
> To: Roy Mendelssohn - NOAA Federal <[hidden email]>
> Cc: David L Carlson <[hidden email]>; R-help <[hidden email]>
> Subject: Re: [R] Reversing one dimension of an array, in a generalized case
>
>
> On 1 Jun 2017, at 22:42, Roy Mendelssohn - NOAA Federal <[hidden email]> 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 <[hidden email]> 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:[hidden email]]
> Sent: Thursday, June 1, 2017 2:00 PM
> To: David L Carlson <[hidden email]>
> Cc: Roy Mendelssohn - NOAA Federal <[hidden email]>; R-help <[hidden email]>
> 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 <[hidden email]> 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:[hidden email]] On Behalf Of Bert Gunter
> Sent: Thursday, June 1, 2017 12:46 PM
> To: Roy Mendelssohn - NOAA Federal <[hidden email]>
> Cc: R-help <[hidden email]>
> 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
> <[hidden email]> 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: [hidden email] 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.
>
> ______________________________________________
> [hidden email] 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.
>
> ______________________________________________
> [hidden email] 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: [hidden email] 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.
>
> ______________________________________________
> [hidden email] 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: [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Reversing one dimension of an array, in a generalized case

David Winsemius
In reply to this post by Roy Mendelssohn - NOAA Federal

> On Jun 1, 2017, at 9:51 AM, Roy Mendelssohn - NOAA Federal <[hidden email]> 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: [hidden email] 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.
>
> ______________________________________________
> [hidden email] 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

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Reversing one dimension of an array, in a generalized case

Bert Gunter-2
In reply to this post by Roy Mendelssohn - NOAA Federal
At the very real risk of flogging a dead horse, I realized that it is
slightly cleaner to use the logical index TRUE in the argument list of
`[` rather than the seq_len(dim[i])  to indicate "everything" in the
ith array dimension. I post on list because maybe this might be a bit
informative to others (as it took me a while to realize it). Here is
the modified version of my function incorporating this variation:

f2 <-function(a, i){
   d <- dim(a)
   l <- as.list(rep(TRUE, length(d))) ## instead of lapply() loop
   l[[i]] <- seq.int(d[i],1)  ## used rev() in prior version
   do.call("[", c(list(a), l))
}


It may also be a teeny tiny bit more efficient to do this way, albeit
unimportantly so.

As this reproduces my prior function, if that was not what you wanted,
neither is this.


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 1:59 PM, Roy Mendelssohn - NOAA Federal
<[hidden email]> wrote:

> 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 <[hidden email]> 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:[hidden email]]
>> Sent: Thursday, June 1, 2017 3:35 PM
>> To: Roy Mendelssohn - NOAA Federal <[hidden email]>
>> Cc: David L Carlson <[hidden email]>; R-help <[hidden email]>
>> Subject: Re: [R] Reversing one dimension of an array, in a generalized case
>>
>>
>> On 1 Jun 2017, at 22:42, Roy Mendelssohn - NOAA Federal <[hidden email]> 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 <[hidden email]> 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:[hidden email]]
>> Sent: Thursday, June 1, 2017 2:00 PM
>> To: David L Carlson <[hidden email]>
>> Cc: Roy Mendelssohn - NOAA Federal <[hidden email]>; R-help <[hidden email]>
>> 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 <[hidden email]> 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:[hidden email]] On Behalf Of Bert Gunter
>> Sent: Thursday, June 1, 2017 12:46 PM
>> To: Roy Mendelssohn - NOAA Federal <[hidden email]>
>> Cc: R-help <[hidden email]>
>> 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
>> <[hidden email]> 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: [hidden email] 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.
>>
>> ______________________________________________
>> [hidden email] 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.
>>
>> ______________________________________________
>> [hidden email] 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: [hidden email] 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.
>>
>> ______________________________________________
>> [hidden email] 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: [hidden email] 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.
>

______________________________________________
[hidden email] 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.