# importing dist matrix into R

**URL:** <https://forum.mothur.org/t/importing-dist-matrix-into-r/1266>\
**Category:** Integrating mothur with other programs\
**Created:** [March 21, 2013, 8:25pm UTC](https://forum.mothur.org/t/importing-dist-matrix-into-r/1266 "2013-03-21T20:25:30Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kendra](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.mothur.org/kendra/32/112_2.png) [@Kendra](https://forum.mothur.org/u/Kendra)\
**Post date:** [March 21, 2013, 8:25pm UTC](https://forum.mothur.org/t/importing-dist-matrix-into-r/1266/1 "2013-03-21T20:25:30Z")

</div>

I’m hoping some R wizard can help me. I’ve constructed distance matrices in mother (yeah for repeated subsampling capability) and now need to use them in R for further analyses. I can get the lower triangle data in but not the labels using which I modified from [Unifrac Distance Matrix -\> Newick using R](https://mothur.ltcmp.net/t/unifrac-distance-matrix-newick-using-r/543/2)

Code: Select all  
b\_bc \<- data.matrix(read.table("", fill=T, row.names=1, skip=1, col.names=1:\<#samples\>))

  
  
I don't get any errors, just don't have the sample labels associated with the data. I've played with the row.names/col.names variable. And, thinking that the problem was related to the lower triangle data form, reran the dist in mothur to get a square matrix but still can't get the labels to be associated with the matrix. do I need to bring the sample names in as a vector then associate that vector with the matrix?

ETA: oh actually something’s not quite right when I load it into R, I get an N x N-1 matrix. Anyone?

---

<div class="post-metadata">

**Author:** ![Kendra](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.mothur.org/kendra/32/112_2.png) [@Kendra](https://forum.mothur.org/u/Kendra)\
**Post date:** [April 8, 2013, 11:25pm UTC](https://forum.mothur.org/t/importing-dist-matrix-into-r/1266/2 "2013-04-08T23:25:38Z")

</div>

ok should anyone else run into this. I shouldn’t have been using col.names. Used header=T instead and it worked beautifully

---

<div class="post-metadata">

**Author:** ![Rich](https://avatars.discourse-cdn.com/v4/letter/r/35a633/32.png) [@Rich](https://forum.mothur.org/u/Rich)\
**Post date:** [June 19, 2013, 2:14pm UTC](https://forum.mothur.org/t/importing-dist-matrix-into-r/1266/3 "2013-06-19T14:14:37Z")

</div>

Hello,

I too have small query.  
Like batch processing is it possible to call mothur command in R script?

  
Thanks

---

<div class="post-metadata">

**Author:** ![linls1912](https://avatars.discourse-cdn.com/v4/letter/l/90db22/32.png) [@linls1912](https://forum.mothur.org/u/linls1912)\
**Post date:** [October 12, 2015, 12:51am UTC](https://forum.mothur.org/t/importing-dist-matrix-into-r/1266/4 "2015-10-12T00:51:02Z")

</div>

you can try, phyloseq::import\_mothur\_dist, if your dist generated from mothur, or if there some other methods?

---

<div class="post-metadata">

**Author:** ![Kendra](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.mothur.org/kendra/32/112_2.png) [@Kendra](https://forum.mothur.org/u/Kendra)\
**Post date:** [June 23, 2016, 7:07pm UTC](https://forum.mothur.org/t/importing-dist-matrix-into-r/1266/5 "2016-06-23T19:07:49Z")

</div>

I’ve been editing my mothur generated dist matrices in excel for the past couple of years but would really love to figure out how to get them into R straight from mothur. What I do in excel is copy the sample names, transpose paste them to be column headers, and add “NA” the the bottom right most cell. After these alterations I can import the dist file using:

```nohighlight
tyc <- read.csv(file="weir.farm.tyc3.csv", header=T, row.names = 1)

```

How would I change my code so I dont’ have to do any alteration in excel?

---

<div class="post-metadata">

**Author:** ![dwaite](https://avatars.discourse-cdn.com/v4/letter/d/ecccb3/32.png) [@dwaite](https://forum.mothur.org/u/dwaite)\
**Post date:** [June 23, 2016, 10:00pm UTC](https://forum.mothur.org/t/importing-dist-matrix-into-r/1266/6 "2016-06-23T22:00:54Z")

</div>

Hey,

I use the following function in R to import mothur distance files:

```nohighlight
parseDistanceMatrix = function(phylip_file) {

 # Read the first line of the phylip file to find out how many sequences/samples it contains
    temp_connection = file(phylip_file, 'r')
    len = readLines(temp_connection, n=1)
    len = as.numeric(len)
    close(temp_connection)
    
 
    phylip_data = read.table(phylip_file, fill=T, row.names=1, skip=1, col.names=1:len)
    phylip_matrix = as.dist(phylip_data)
    return(phylip_matrix)
}

```

Funnily enough, it’s actually based on your original R command in this thread.

Using this does with you a warning when it does the as.dist() cast, but I think this is just due to the trailing tab on the last line of the phylip. I’ve tested it extensively and it doesn’t change the data in anyway.

---

<div class="post-metadata">

**Author:** ![Kendra](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.mothur.org/kendra/32/112_2.png) [@Kendra](https://forum.mothur.org/u/Kendra)\
**Post date:** [June 24, 2016, 5:27pm UTC](https://forum.mothur.org/t/importing-dist-matrix-into-r/1266/7 "2016-06-24T17:27:52Z")

</div>

awesome! thanks. it’s the circle of code life 🙂

---

<div class="post-metadata">

**Author:** ![Kendra](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.mothur.org/kendra/32/112_2.png) [@Kendra](https://forum.mothur.org/u/Kendra)\
**Post date:** [August 3, 2016, 2:42pm UTC](https://forum.mothur.org/t/importing-dist-matrix-into-r/1266/8 "2016-08-03T14:42:50Z")

</div>

I tweaked your code a bit because I like working with df rather than dist (I can remove samples with logic vectors when they’re df)

```nohighlight
parseDistanceDF = function(phylip_file) {

   # Read the first line of the phylip file to find out how many sequences/samples it contains
    temp_connection = file(phylip_file, 'r')
    len = readLines(temp_connection, n=1)
    len = as.numeric(len)
    len = len +1
    close(temp_connection)
   
   
    phylip_data = read.table(phylip_file, fill=T, row.names=1, skip=1, col.names=1:len)
    colnames(phylip_data) <- row.names(phylip_data)
    return(phylip_data)
}

```
