Skip to content
Prev 369550 / 398503 Next

Reversing one dimension of an array, in a generalized case

Not sure it is "obvious", but this function implements what you describe:

revdim <- function( a, d ) {
    dims <- attr( a, "dim" )
    idxs <- lapply( seq_along( dims )
                  , function( dd ) {
                      if ( d == dd ) seq.int( dims[ dd ], 1, -1 )
                      else seq.int( dims[ dd ] )
                    }
                  )
    do.call( `[`, c( list( a ), idxs ) )
}

revdim( junk1, 2 )
On Thu, 1 Jun 2017, Roy Mendelssohn - NOAA Federal wrote:

            
---------------------------------------------------------------------------
Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<jdnewmil at dcn.davis.ca.us>        Basics: ##.#.       ##.#.  Live Go...
                                       Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k