Zaynab
Zaynab

Reputation: 233

eigenvalues and eigenvectors in R

When I have tried to compute eigenvalues and eigenvectors for the following matrix using eigen() function, contrary my expectations, the smallest eigenvalue is not zero, why?

 > lapMatrix
      [,1] [,2] [,3] [,4]
 [1,]  0.6 -0.1 -0.5  0.0
 [2,] -0.1  1.0  0.0 -0.9
 [3,] -0.5  0.0  1.0 -0.5
 [4,]  0.0 -0.9 -0.5  1.4

 > eigen(lapMatrix)
  $values
 [1] 2.277671e+00 1.241300e+00 4.810288e-01 1.297990e-16

 $vectors
            [,1]       [,2]       [,3] [,4]
 [1,]  0.1359786  0.5205445  0.6786333  0.5
 [2,] -0.5412623  0.3853145 -0.5554889  0.5
 [3,] -0.3480021 -0.7447136  0.2725734  0.5
 [4,]  0.7532858 -0.1611455 -0.3957179  0.5

Upvotes: 1

Views: 2157

Answers (1)

agstudy
agstudy

Reputation: 121568

You can simply round the result:

round(eigen(mm)$values,2)
## [1] 2.28 1.24 0.48 0.00

You can wrap this within a small function :

reigen <- 
  function(...,digits =2) round(eigen(...)$values,digits)

Or using zapsmall like suggested in the comment ( it will determine automatically the number of digits):

reigen <- 
  function(...,digits =2) zapsmall(eigen(...)$values)

reigen(mm)
[1] 2.278 1.241 0.481 0.000

Upvotes: 1

Related Questions