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

Berton Gunter gunter.berton at gene.com
Wed Apr 5 01:11:47 CEST 2006


temp is a data.frame, not an array. 

Have you read "An Introduction to R"? If not, please do so before posting.
You might also save yourself a lot of time if you learned to use R's search
facilties, (help.search(), RSiteSearch()). R is a mature product with an
incredible amount of excellent code already available. These facilities help
you to find out, though I admit it can be frustrating at times to get the
right keyword. Don't neglect Google either.

If I'm not mistaken, you are trying to redo what already has been done in
the cluster package.

library(cluster)
?dist
where you want method='manhattan'.

If so, you surely are wasting your time and producing poor code, as the c
code will be orders of magnitude faster. 

-- Bert Gunter
Genentech Non-Clinical Statistics
South San Francisco, CA
 
"The business of the statistician is to catalyze the scientific learning
process."  - George E. P. Box
 
 

> -----Original Message-----
> From: r-help-bounces at stat.math.ethz.ch 
> [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Russ Branaghan
> Sent: Tuesday, April 04, 2006 3:42 PM
> To: r-help at stat.math.ethz.ch
> Subject: [R] Help on computing a matrix from another matrix in R
> 
> 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? 
> 
> Thanks in advance,
> 
> Russ
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! 
> http://www.R-project.org/posting-guide.html
>




More information about the R-help mailing list