[R] using eigen function in MAP and purr

John Fox j|ox @end|ng |rom mcm@@ter@c@
Tue Mar 30 00:53:27 CEST 2021


Dear V. K. Chetty,

Perhaps I'm missing something but why wouldn't you just use a list of 
matrices, as in the following?

---------- snip ---------

 > set.seed(123) # for reproducibility

 > (Matrices <- lapply(1:3, function(i) matrix(sample(1:50, 4), 2, 2)))

[[1]]
      [,1] [,2]
[1,]   31   14
[2,]   15    3

[[2]]
      [,1] [,2]
[1,]   42   37
[2,]   43   14

[[3]]
      [,1] [,2]
[1,]   25   27
[2,]   26    5

 > (Eigenvalues <- lapply(Matrices, function(x) eigen(x, 
only.values=TRUE)$values))

[[1]]
[1] 37.149442 -3.149442

[[2]]
[1]  70.27292 -14.27292

[[3]]
[1]  43.3196 -13.3196

---------- snip ---------

I hope this helps,
  John

John Fox, Professor Emeritus
McMaster University
Hamilton, Ontario, Canada
web: https://socialsciences.mcmaster.ca/jfox/

On 2021-03-29 5:28 p.m., Veerappa Chetty wrote:
> I want to use map and purr functions to compute eigen values for 3000
> matrices. Each matrix has 2 rows and 2 columns. The following code does not
> work.
> 
> test.dat<- tibble(ID=c(1,2),a=c(1,1),b=c(1,1),c=c(2,2),d=c(4,3))
> 
> test.out<-test.dat %>% nest(-ID) %>% mutate(fit = purrr::map(data,~
> function(x) eigen(matrix(x,2,2)), data=.))
> 
> This must be a trivial question for current young practitioners ( In my 9
> th decade, I am having fun using R markdown and I am trying to continue my
> research!) I would greatly appreciate any help.
> Thanks.
> V.K.Chetty
>



More information about the R-help mailing list