Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8873341
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T18:26:29+00:00 2026-06-14T18:26:29+00:00

This should be relatively simple but for some reason, I’m coming up stumped. I

  • 0

This should be relatively simple but for some reason, I’m coming up stumped.

I want to reorder a phylogenetic distance matrix so that the columns are in alphabetical order.

Here is a quick example:

require("ape")
set.seed(5)
dist(cophenetic(rtree(5)))

Any ideas?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T18:26:31+00:00Added an answer on June 14, 2026 at 6:26 pm

    if T <- dist(cophenetic(rtree(5)))

    T is of class dist so it doesnt have names, just labels.
    You can order its labels

    ordering <- sort(attr(T, "Labels"))
    

    You can coerce it into a matrix, and then sort it

    T.mat <- as.matrix(T)[ordering, ordering]
    

    Then as @Roland suggested, convert it back to dist

    T  <- as.dist(T.mat)
    

    If you would like to control the format of the matrix, you can use the following

    T.fm <- format(T.mat, digits=3, nsmall=3)
    T.fm[row(T.fm) < col(T.fm)] <- ""
    print(T.fm, quote=F)
    
    #      t1    t2    t3    t4    t5   
    # t1 0.000                        
    # t2 3.436 0.000                  
    # t3 1.309 3.632 0.000            
    # t4 2.145 2.338 2.530 0.000      
    # t5 1.756 2.783 2.173 1.136 0.000
    
    T  <- as.dist(T.fm)
    T
    #       t1    t2    t3    t4
    # t2 3.436                  
    # t3 1.309 3.632            
    # t4 2.145 2.338 2.530      
    # t5 1.756 2.783 2.173 1.136
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This seems like it should be fairly simple, but for some reason I can't
Got stuck and need some help, This should be relatively simple but it's been
This seems like it should be quite simple, but for some reason I can't
This should be relatively simple to do but I've yet to find a description
Setup OSX 10.7.4 Eclipse Indigo, Pydev, Tweepy-1.9 I thought this should be relatively simple
This should be simple, but I'm getting confused. I have a parent/child tables -
This should be super simple. The first code block works, but when I wrap
This should be quite simple, but I can't work out the syntax. I have
I think this is a relatively simple question, but I don't precisely know what's
This should be relatively simple, here is my situation; I have copied the trunk

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.