Rilja - 5 months ago 32

R Question

I've got this matrix Q:

`[,1] [,2] [,3] [,4]`

[1,] 0.77325281 0.04054297 0.5 0.3878613

[2,] 0.01478692 -0.61477862 0.5 -0.6097775

[3,] -0.60774649 -0.19012961 0.5 0.5869369

[4,] -0.18029324 0.76436526 0.5 -0.3650207

I've tried to extract the first two columns of this matrix with:

`Q[,c(1,2)]`

Then I get this error:

`Error in Q[, c(1, 2)] : incorrect number of dimensions`

Can anyone tell me why I get this error and how to get the first two columns?

How I computed Q:

`test <- matrix(c(0, 4.05, 8.25, 5.57, 4.05, 0, 2.54, 2.69, 8.25, 2.54, 0, 2.11, 5.57, 2.69, 2.11, 0), nrow = 4, ncol = 4)`

# matrix of squared dissimilarities

test2 <- matrix(c(test^2), nrow = 4, ncol = 4)

# double centering and multiplied

n=4

I=diag(n)

J=matrix(1,n,n)

C=I-J/n

B <- ((-.5)*C%*%test2%*%C)

B # double centered matrix

# eigenvalue decomposition

Q = eigen(B)

Answer

**edit** since question update

If you look at the structure of the object that `eigen`

returns you will see it is a list of length two

```
str(Q)
# List of 2
# $ values : num [1:4] 3.57e+01 3.27 -1.95e-14 -5.57
# $ vectors: num [1:4, 1:4] 0.7733 0.0148 -0.6077 -0.1803 0.0405 ...
```

You can extract the eigenvectors with `Q$vectors`

This is described in the help page of `?eigen`

So for your question use

```
Q$vectors[, 1:2]
# [,1] [,2]
# [1,] 0.77325281 0.04054297
# [2,] 0.01478692 -0.61477862
# [3,] -0.60774649 -0.19012961
# [4,] -0.18029324 0.76436526
```