[R] Some difficulties to use the apply command on an array

Laurent Rhelp L@urentRHe|p @end|ng |rom |ree@|r
Mon Jul 5 21:57:09 CEST 2021


Very interesting
Thank you


Le 05/07/2021 à 17:31, Jeff Newmiller a écrit :
> apply _always_ returns a matrix of vector results. That is, the result of each call to FUN may be a matrix, but a matrix is merely a vector with dimensions that are in this case ignored. So restore the dimensions on the result:
>
> array(apply(x, 1, FUN = function(x) diag( svd(x)$d )), dim=c(3,3,5))
>
>
> On July 5, 2021 8:15:16 AM PDT, Eric Berger <ericjberger using gmail.com> wrote:
>> Hi Laurent,
>> I am not sure how to get apply()  to work but the following uses
>> lapply()
>> and returns the matrices in a list.
>>
>> lapply(1:3, FUN = function(i) diag( svd(x[i,,])$d ))
>>
>> HTH,
>> Eric
>>
>> On Mon, Jul 5, 2021 at 5:56 PM Laurent Rhelp <LaurentRHelp using free.fr>
>> wrote:
>>
>>> Dear R-Help,
>>>
>>> I have an array x made up of three matrices of 5 rows and 3 columns
>> of
>>> complex numbers (the complex numbers are not the problem)
>>>
>>> ## my array
>>> x <- structure(c(5.6196790161893828+0i, 5.7565523942393364+0i,
>>> 8.5242834298729342+0i,
>>>                 10.304766710160479+0i, 11.412967010108229+0i,
>>> -2.6952197604866495-5.7324226520260237e-18i,
>>>                 -3.8053698833683476-1.5535793240880411i,
>>> -4.440850591952894-0.21277190153473785i,
>>>                 -4.9049897326853316+0.22487294128201613i,
>>> -4.6982778900868931-0.35986943359186585i,
>>>                 -6.7722948827866034-1.8252881225149525e-16i,
>>> -5.6864255243941733-0.82149125972147463i,
>>>                 -2.5321753317962115-3.4309121438578525i,
>>> 0.85377696343414344-4.3720244641138883i,
>>>                 6.3363344379954638-2.23654533694011i,
>>> -2.6952197604866495+5.7324226520260237e-18i,
>>>                 -3.8053698833683476+1.5535793240880411i,
>>> -4.440850591952894+0.21277190153473785i,
>>>                 -4.9049897326853316-0.22487294128201613i,
>>> -4.6982778900868931+0.35986943359186585i,
>>>                 3.1683154084671417+0i, 5.397392162921844+0i,
>>> 8.3085887222115922+0i,
>>>                 11.150391891976289+0i, 13.173434845070231+0i,
>>> 3.7978840709983359+5.0735539531499429e-17i,
>>>                 3.0599215005995717-2.3669737981128267i,
>>> 0.5951827523696811-3.2116907301709845i,
>>>                 -1.7735244105472532-3.8366652813316566i,
>>> -5.0558348297797169-2.6645747173403049i,
>>>                 -6.7722948827866034+1.8252881225149525e-16i,
>>> -5.6864255243941733+0.82149125972147463i,
>>>                 -2.5321753317962115+3.4309121438578525i,
>>> 0.85377696343414344+4.3720244641138883i,
>>>                 6.3363344379954638+2.23654533694011i,
>>> 3.7978840709983359-5.0735539531499429e-17i,
>>>                 3.0599215005995717+2.3669737981128267i,
>>> 0.5951827523696811+3.2116907301709845i,
>>>                 -1.7735244105472532+3.8366652813316566i,
>>> -5.0558348297797169+2.6645747173403049i,
>>>                 8.5581082281979697+0i, 8.6908832216086331+0i,
>>> 12.394094469562258+0i,
>>>                 16.106350896659897+0i, 19.569513600539693+0i), .Dim =
>>> c(5L, 3L,
>>> 3L))
>>>
>>> str(x)
>>> #  cplx [1:5, 1:3, 1:3] 5.62+0i 5.76+0i 8.52+0i ...
>>>
>>> The dimensions (i,j,k) of my array are 5, 3, 3. For every value of i,
>> I
>>> am interested in the squared matrices made up of the j columns for
>> all
>>> the k indice. For a given value i I have to keep the row i for every
>> k.
>>> For example if i=1, I am interested by the matrix x[1,,]
>>> ##
>>> x[1,,]
>>>
>>> ##              [,1]         [,2]         [,3]
>>> ## [1,]  5.619679+0i -2.695220+0i -6.772295+0i
>>> ## [2,] -2.695220-0i  3.168315+0i  3.797884-0i
>>> ## [3,] -6.772295-0i  3.797884+0i  8.558108+0i
>>>
>>> Now, for every value of i, I want to calculate the SVD decomposition
>> and
>>> get the diagonal matrix created by the singular values. When I set
>> the i
>>> value it works:
>>> diag( svd(x[1,,])$d )
>>>
>>> # [,1]     [,2]      [,3]
>>> # [1,] 15.73833 0.000000 0.0000000
>>> # [2,]  0.00000 1.502771 0.0000000
>>> # [3,]  0.00000 0.000000 0.1049992
>>>
>>>
>>> But when I try to do a loop on the i value it doesn't work:
>>>
>>> apply(x, c(2,3), FUN = function(x) diag( svd(x)$d ))
>>>
>>>
>>> # [,1]        [,2]        [,3]
>>> # [1,] Numeric,361 Numeric,81  Numeric,144
>>> # [2,] Numeric,81  Numeric,400 Numeric,81
>>> # [3,] Numeric,144 Numeric,81  Numeric,900
>>>
>>> I do not understand how to do the apply on an array in order to get
>> my
>>> diagonals matrices. I think that I miss something about the
>>> understanding of the array object.
>>>
>>> Thank you for your help
>>>
>>> Laurent
>>>
>>>
>>>
>>>
>>> --
>>> L'absence de virus dans ce courrier électronique a été vérifiée par
>> le
>>> logiciel antivirus Avast.
>>> https://www.avast.com/antivirus
>>>
>>> ______________________________________________
>>> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>> PLEASE do read the posting guide
>>> http://www.R-project.org/posting-guide.html
>>> and provide commented, minimal, self-contained, reproducible code.
>>>
>> 	[[alternative HTML version deleted]]
>>
>> ______________________________________________
>> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide
>> http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.



-- 
L'absence de virus dans ce courrier électronique a été vérifiée par le logiciel antivirus Avast.
https://www.avast.com/antivirus



More information about the R-help mailing list