Dear Jarrett,

I am not sure what it is that you are trying to accomplish with the last two 
lines. Isn't 'dists' already what you want? If you just want to set the 
diagonal of 'dists' to NA, then 'diag(dists) <- NA' would do that. But maybe I 
am missing the point here entirely.

Best,
Wolfgang

>-----Original Message-----
>From: R-sig-phylo [mailto:r-sig-phylo-boun...@r-project.org] On Behalf Of 
>Jarrett
>Phillips
>Sent: Sunday, 05 February, 2023 8:31
>To: r-sig-phylo@r-project.org
>Subject: [R-sig-phylo] Generating a matrix of interspecfic genetic distances
>
>Hi All,
>
>I am using the ape R package to extract interspecific distances from a
>pairwise distance matrix.
>
>Consider the woodmouse example.
>
>    library(ape)
>
>    data(woodmouse)
>
>    dists <- dist.dna(woodmouse, model = "raw", as.matrix = TRUE)
>    isSymmetric(dists) # TRUE
>    dists[upper.tri(dists, diag = FALSE)] <- NA # replace upper triangle
>with NA and then subset to get lower triangular matrix
>    isSymmetric(dists) # FALSE
>    dists # this is a matrix
>
>    labs <- labels(woodmouse) # get species names
>
>    res <- outer(labs, labs, "==") # get cases where matrix elements are
>equal
>    inter <- na.omit(dists[!res]) # get interspecific distances by taking
>elements that are not equal
>
>The problem I am facing is that I need 'inter' to be a matrix instead of a
>vector, so that I can tell which distances belong to which pairs of
>specimens. The 'inter' matrix should have the same row and column names as
>the 'dists' variable. Simply doing
>
>    as.matrix(inter)
>
>will not suffice in this case since row names and column names are not
>preserved.
>
>Once I have a matrix of only interspecific distances, I can then find
>nearest neighbours for all species in my dataset and continue from there.
>
>I feel it is an easy solution, but something is just not clicking.
>
>Any ideas?
>
>Please let me know if anything is unclear.
>
>Thanks.
>
>Cheers,
>
>Jarrett

_______________________________________________
R-sig-phylo mailing list - R-sig-phylo@r-project.org
https://stat.ethz.ch/mailman/listinfo/r-sig-phylo
Searchable archive at http://www.mail-archive.com/r-sig-phylo@r-project.org/

Reply via email to