# [R] Help on computing a matrix from another matrix in R

Russ Branaghan rbranaghan at bigredrooster.com
Wed Apr 5 00:42:23 CEST 2006

```Hello All,

I am new to R and am having a problem.

I have the following 29 x 5 array called temp with a mode "list".

Sincere Exciting Competent Sophisticated Rugged
3.5      2.5       3.8           3.5    3.0
2.0      2.5       3.5           2.5    2.3
2.8      3.0       2.3           1.5    1.8
2.0      4.0       2.8           1.5    4.5
2.5      1.5       1.8           1.3    1.3
4.0      2.0       3.0           2.3    2.5
3.0      1.8       3.0           3.0    2.0
1.7      1.7       1.3           1.0    1.3
2.7      2.3       1.5           1.3    1.8
2.8      1.0       3.5           4.0    1.8
3.3      1.5       3.0           2.3    1.5
2.3      1.3       1.3           1.0    1.0
2.0      1.3       1.0           1.3    1.0
3.0      2.0       2.0           2.3    1.3
2.5      1.5       2.0           1.0    1.5
2.5      1.3       2.8           2.8    1.5
2.3      1.5       1.5           1.3    1.3
2.0      2.3       2.3           1.8    2.8
2.0      3.3       3.0           2.3    3.8
2.8      3.8       4.0           2.3    4.8
2.8      3.8       3.0           2.3    4.3
2.5      2.0       3.0           2.8    2.0
2.3      3.0       1.8           1.5    2.0
3.0      3.3       3.3           2.8    2.3
3.5      2.8       3.3           1.8    2.0
5.0      1.5       1.5           1.3    1.3
2.7      1.7       1.5           1.3    1.5
1.8      1.3       1.3           1.0    1.0
2.8      4.5       3.8           3.0    4.0

I am trying to compute the similarity of each row with each other row by
summing the absolute values of the differences between each item in each row
with each item in each other row. So for example since row 1 is 3.5, 2.5,
3.8, 3.5, 3.0 and row 2 is 2.0, 2.5, 3.5, 2.5, and 2.3, the sum of the
absolute values of the differences is 3.5. This is given by this code:

sum(abs(temp[1,]-temp[2,]))

My issue is that I want a 29 x 29 matrix of these similarity scores. I have
made some progress, but I cannot quite get it. The closest code I have is
the following (all on one line, of course):

ans<- for (firstrow in 1:29) { for (escrow in 1:29) {
print(sum(abs(temp[firstrow,] - temp[secrow,]) ) ) } }

I realize that print is not the right function to be using, but at least it
displays my results in a long list. Also, ans has the following
characteristics:

> ans
[1]

> dim(ans)
NULL

> mode(ans)
1] "numeric"

Can someone help me by telling me how to get this into a 29 x 29 matrix of
the right mode?