[R] Write lower half of distance matrix only

Martin Maechler maechler at stat.math.ethz.ch
Sat Jul 26 10:01:31 CEST 2008


>>>>> "jh" == jim holtman <jholtman at gmail.com>
>>>>>     on Fri, 25 Jul 2008 12:34:49 -0400 writes:
>>>>> "jh" == jim holtman <jholtman at gmail.com>
>>>>>     on Fri, 25 Jul 2008 12:34:49 -0400 writes:

    jh> Does this do what you want:

    >> x <- matrix(1:25,5)
    >> x
    jh>       [,1] [,2] [,3] [,4] [,5]
    jh> [1,]    1    6   11   16   21
    jh> [2,]    2    7   12   17   22
    jh> [3,]    3    8   13   18   23
    jh> [4,]    4    9   14   19   24
    jh> [5,]    5   10   15   20   25
    >> x[upper.tri(x)] <- NA
    >> write.table(x, na="", row.names=FALSE, col.names=FALSE)
    jh> 1
    jh> 2 7
    jh> 3 8 13
    jh> 4 9 14 19
    jh> 5 10 15 20 25
    >> 

Yes, but why not use the S / R  objects / methods that were
exactly written for the purpose?

I strongly recommend to make your matrix into a "dist" object
(or a "dissimilarity" object -- the latter from package
'cluster', using function daisy(.)) !

 > m <- matrix(1:25,5); m <- m + t(m); diag(m) <- 0
 > (D <- as.dist(m))

    1  2  3  4
 2  8         
 3 14 20      
 4 20 26 32   
 5 26 32 38 44

Note that the above does *not* print the  all-0 diagonal
and so saves printing space.

Martin Maechler, ETH Zurich


    jh> On Fri, Jul 25, 2008 at 8:30 AM, pgseye <prseye at gmail.com> wrote:
    >> 
    >> Thanks Jim,
    >> 
    >> The morphometry software I mentioned outputs distance matrices in the
    >> format:
    >> 
    >> 0.0000
    >> 7.1598 0.0000
    >> 8.7241 8.3506 0.0000
    >> 9.5217 7.2457 2.5642 0.0000
    >> 
    >> I would have liked to write to a file the same type of matrix in R, to
    >> ensure the same data format. Mantel for Windows is optimally setup to take
    >> this type of matrix.
    >> 
    >> Thanks
    >> 
    >> 
    >> 
    >> 
    >> jholtman wrote:
    >>> 
    >>> How do you want the lower half written out?  can you give us an
    >>> example of the input matrix and then what you would expect to see on
    >>> the output.  Is it still a matrix with the upper half set to zero/NA?
    >>> Do you want it as a vector?  What is the other program expecting as
    >>> input?
    >>> 
    >>> You need to provide more information so that we can provide suggestions.
    >>> 
    >>> On Fri, Jul 25, 2008 at 7:03 AM, pgseye <prseye at gmail.com> wrote:
    >>>> 
    >>>> Hi,
    >>>> 
    >>>> I'm very new to R. I want to know if there is any way to write only the
    >>>> lower half of a distance matrix created in R to a csv file for example.
    >>>> I
    >>>> get the 'cannot coerce class "dist" into a data.frame' message when I
    >>>> try.
    >>>> I have used as.matrix and can write to a file this way, but as a full
    >>>> matrix.
    >>>> 
    >>>> The reason I only want the lower half is that I've been doing some Mantel
    >>>> correlations with distance matrices (of Procrustes distances) generated
    >>>> by
    >>>> other (geometric morphometric) software that are in this format. However,
    >>>> I'm needing to use an alternative such as R to calculate distance
    >>>> matrices
    >>>> of Fourier coefficients which the other software can't do.  So far, the
    >>>> Mantel correlations calculated from these two matrices (with R and also
    >>>> another software (Mantel for Windows)) are slightly different (when they
    >>>> should be exactly the same) and I'm wondering whether it's because the
    >>>> input
    >>>> format is different (ie one full, one half).
    >>>> 
    >>>> Thanks,
    >>>> 
    >>>> Paul
    >>>> 
    >>>> 
    >>>> --
    >>>> View this message in context:
    >>>> http://www.nabble.com/Write-lower-half-of-distance-matrix-only-tp18649556p18649556.html
    >>>> Sent from the R help mailing list archive at Nabble.com.
    >>>> 
    >>>> ______________________________________________
    >>>> R-help at r-project.org mailing list
    >>>> https://stat.ethz.ch/mailman/listinfo/r-help
    >>>> PLEASE do read the posting guide
    >>>> http://www.R-project.org/posting-guide.html
    >>>> and provide commented, minimal, self-contained, reproducible code.
    >>>> 
    >>> 
    >>> 
    >>> 
    >>> --
    >>> Jim Holtman
    >>> Cincinnati, OH
    >>> +1 513 646 9390
    >>> 
    >>> What is the problem you are trying to solve?
    >>> 
    >>> ______________________________________________
    >>> R-help at r-project.org mailing list
    >>> https://stat.ethz.ch/mailman/listinfo/r-help
    >>> PLEASE do read the posting guide
    >>> http://www.R-project.org/posting-guide.html
    >>> and provide commented, minimal, self-contained, reproducible code.
    >>> 
    >>> 
    >> 
    >> --
    >> View this message in context: http://www.nabble.com/Write-lower-half-of-distance-matrix-only-tp18649556p18650794.html
    >> Sent from the R help mailing list archive at Nabble.com.
    >> 
    >> ______________________________________________
    >> R-help at r-project.org mailing list
    >> https://stat.ethz.ch/mailman/listinfo/r-help
    >> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
    >> and provide commented, minimal, self-contained, reproducible code.
    >> 



    jh> -- 
    jh> Jim Holtman
    jh> Cincinnati, OH
    jh> +1 513 646 9390

    jh> What is the problem you are trying to solve?

    jh> ______________________________________________
    jh> R-help at r-project.org mailing list
    jh> https://stat.ethz.ch/mailman/listinfo/r-help
    jh> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
    jh> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list