Yves REECHT
2010-Nov-10 09:16 UTC
[R] force apply not to drop the dimensions of FUN results ?
Dear R users,
Here is my problem:
I have an array with at least four dimensions:
> dim(myArray)
[1] 20 17 3 6
I'd like to apply a function to each occurrence of the matrix (3x6)
defined by the last two dimensions. This interpolation function always
return a matrix of the same dimensions as its argument:
> interpSecteurs.f(myArray[1, 1, , ])
secteur
rotation 1 2 3 4 5 6
1 3 5.25 10 6 6 3
2 3 10.00 10 7 7 3
3 3 0.50 10 8 8 3
But when applying this function with apply, these matrices are coerced
in vectors of length 18:
> test <- apply(myArray,
+ c(1, 2),
+ interpSecteurs.f)
> dim(test)
[1] 18 20 17
I read in the help that this is the normal behaviour in apply.
However, I really need to keep the same dimension as the input data.
Do you know any function or method which allows bypassing this behaviour ?
(unfortunately my function seems not suitable for a vectorization using
sweep)
Alternatively, a method to recover the wished array dimensions afterward
would be also acceptable.
Many thanks in advance.
Yves
--
---------------------------------------------------
Yves REECHT
Dpt STH (Sciences et Technologies Halieutiques)
IFREMER, BP 70
29280 PLOUZANE
FRANCE
tel. +33 (0)2 98 22 43 69
------------------------------------------)><))))°>
[[alternative HTML version deleted]]
Claudia Beleites
2010-Nov-10 13:09 UTC
[R] force apply not to drop the dimensions of FUN results ?
Dear Yves, You may not need to do more than set the dim attribute correctly: dim (test) <- c (dim (myArray) [c (3 : 4, 1 : 2)] or dim (test) <- c (dim (myArray) [c (4 : 3, 1 : 2)] Claudia -- Claudia Beleites Dipartimento dei Materiali e delle Risorse Naturali Universit? degli Studi di Trieste Via Alfonso Valerio 6/a I-34127 Trieste phone: +39 0 40 5 58-37 68 email: cbeleites at units.it
Yves.Reecht at ifremer.fr
2010-Nov-10 20:34 UTC
[R] force apply not to drop the dimensions of FUN results ?
Thank you very much Claudia, That (the first form) works perfectly. Easily made generic using head and tail on the dim attribute. All the best, Yves Claudia Beleites <cbeleites at units.it> a ?crit?:> Dear Yves, > > You may not need to do more than set the dim attribute correctly: > > dim (test) <- c (dim (myArray) [c (3 : 4, 1 : 2)] > or > dim (test) <- c (dim (myArray) [c (4 : 3, 1 : 2)] > > Claudia > > > -- > Claudia Beleites > Dipartimento dei Materiali e delle Risorse Naturali > Universit? degli Studi di Trieste > Via Alfonso Valerio 6/a > I-34127 Trieste > > phone: +39 0 40 5 58-37 68 > email: cbeleites at units.it >