# [R] transform matrix

Sean Davis sdavis2 at mail.nih.gov
Tue Nov 29 14:00:38 CET 2005

```On 11/29/05 7:47 AM, "Robert" <samrobertsmith at yahoo.com> wrote:

>
>
> Sean Davis <sdavis2 at mail.nih.gov> wrote:
>
> On 11/29/05 7:42 AM, "Robert" wrote:
>
>>
>>
>> Sean Davis wrote:
>>
>> On 11/29/05 6:46 AM, "Robert" wrote:
>>
>>> I run the following code and got a wrong message. Anyway to transform test1
>>> to
>>> a 6 by 6 matrix? many thanks!
>>>> test1
>>> 1 2 3 4 5 6
>>> 1 0.0000000 0.7760856 2.022222 0.6148687 3.0227028 3.2104434
>>> 2 0.7760856 0.0000000 1.690790 0.2424415 2.3636083 2.5334957
>>> 3 2.0222216 1.6907899 0.000000 1.5939158 1.5126344 1.7304217
>>> 4 0.6148687 0.2424415 1.593916 0.0000000 2.4265906 2.6085845
>>> 5 3.0227028 2.3636083 1.512634 2.4265906 0.0000000 0.2184739
>>> 6 3.2104434 2.5334957 1.730422 2.6085845 0.2184739 0.0000000
>>>> tryD=matrix(data = test1, nrow = 6, ncol = 6)
>>> Warning message:
>>> Replacement length not a multiple of the elements to replace in matrix(...)
>>
>> What is test1? Try:
>>
>> tryD <- as.matrix(test1)
>>
>> Does that do it?
>>
>> Sean
>>
>>
>>
>>
>> It works if I use tryD <- as.matrix(test1).
>> But why tryD=matrix(data = test1, nrow = 6, ncol = 6) wrong?
>
> What is test1? What are the results of:
>
> class(test1)
> dim(test1)
>
> Sean
>
>
>
>
>
>> class(test1)
> [1] "dist"
>> dim(test1)
> NULL

If you read the help for "dist", you will see that objects of class "dist"
have some other information besides the matrix itself (which is actually
stored in upper triangular form only, I believe.  So, while the "show"
method for "dist" objects shows the matrix, it isn't really a matrix in
memory, but a "dist" object.  There happens to be a coercion function to go
from "dist" to "matrix" which is why the as.matrix method works.

Sean

```