Please also reply to the list, not just to me. Unfortunately, I still don't get it. Can you show what you want the *final* matrix to look like for this specific example?
Best, Wolfgang >-----Original Message----- >From: Jarrett Phillips [mailto:phillipsjarre...@gmail.com] >Sent: Sunday, 05 February, 2023 18:30 >To: Viechtbauer, Wolfgang (NP) >Subject: Re: [R-sig-phylo] Generating a matrix of interspecfic genetic >distances > >Thanks Wolfgang, > >‘dists’ contains both intraspecific and interspecific comparisons. The ‘inter’ >variable extracts interspecific values from ‘dists’, but returns a vector >instead >of a matrix. > >I’m wondering how (if it is possible) I can return ‘inter’ as a matrix. The >result likely will not be a square matrix, which is okay. > >As mentioned in my previous email, as.matrix(inter) simply returns a matrix >with >a single column, so destroys the block structure of matrices, and also drops >relevant row and column names found in ‘dists’. > >‘res’ is used to return all elements of ‘dists’ except the main diagonal. > >If things are still unclear, please let me know. > >Cheers, > >Jarrett > >On Sun, Feb 5, 2023 at 6:10 AM Viechtbauer, Wolfgang (NP) ><wolfgang.viechtba...@maastrichtuniversity.nl> wrote: >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/