gt;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
>>
>>
> On Jun 1, 2017, at 9:51 AM, 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:
mailto:sezenism...@gmail.com]
> Sent: Thursday, June 1, 2017 3:35 PM
> To: Roy Mendelssohn - NOAA Federal
> Cc: David L Carlson ; R-help
> Subject: Re: [R] Reversing one dimension of an array, in a generalized case
>
>
> On 1 Jun 2017, at 22:42, Roy Mendelssohn - NOA
eral
Cc: David L Carlson ; R-help
Subject: Re: [R] Reversing one dimension of an array, in a generalized case
On 1 Jun 2017, at 22:42, Roy Mendelssohn - NOAA Federal
wrote:
Thanks to all for responses/. There was a question of exactly what was wanted.
It is the generalization of the obvious
t;> [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.444 >"
>>> [2] "Mean relative difference: 0.1262209"
Component “dim”: Mean relative difference: 0.444 >"
>> [2] "Mean relative difference: 0.1262209"
>>> all.equal(f(z,4),f2(z,4))
>> [1] "Attributes: < Component “dim”: Mean relative difference: 0.5714286 >"
>&g
0.5855162"
>
> David C
>
> -Original Message-----
> From: Bert Gunter [mailto:bgunter.4...@gmail.com]
> Sent: Thursday, June 1, 2017 2:00 PM
> To: David L Carlson
> Cc: Roy Mendelssohn - NOAA Federal ; R-help
>
> Subject: Re: [R] Reversing one dimen
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
>>
>&g
m: Bert Gunter [mailto:bgunter.4...@gmail.com]
Sent: Thursday, June 1, 2017 2:00 PM
To: David L Carlson
Cc: Roy Mendelssohn - NOAA Federal ; R-help
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]
s A&M University
> College Station, TX 77840-4352
>
>
>
>
>
>
> -Original Message-
> From: R-help [mailto:r-help-boun...@r-project.org] On Behalf Of Bert Gunter
> Sent: Thursday, June 1, 2017 12:46 PM
> To: Roy Mendelssohn - NOAA Federal
> Cc: R-h
June 1, 2017 12:46 PM
To: Roy Mendelssohn - NOAA Federal
Cc: R-help
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]])
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 se
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 ver
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
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 sec
15 matches
Mail list logo