Search code examples
rvegan

How do I manipulate a distance object in r to extract just the values I want?


I'm trying to conduct an analysis where I need to know the relative change in community composition over time using Sørensen's or bray-Curtis distance. I have a matrix of plots that looks like this:

> example
plot species_a species_b species_c
1    1      0.16      0.25      0.00
2    2      0.00      1.00      0.00
3    3      0.70      0.00      0.25
4    1      1.00      0.00      0.00
5    2      0.10      0.40      0.50
6    3      0.30      0.30      0.30
7    1      0.20      0.20      0.60
8    2      0.11      0.12      0.13
9    3      0.00      0.00      0.90

In this example data I have 3 plots, measured at 3 different points in time, in my real data I have 850 plots measured at 3 different points in time. I can easily calculate a distance matrix using r and the vegan package function vegdist:

 example<-example[,-1]
 di<-vegdist(example, method="bray")
          1         2         3         4         5         6         7         8
2 0.6453901                                                                      
3 0.7647059 1.0000000                                                            
4 0.7730496 1.0000000 0.2820513                                                  
5 0.5035461 0.6000000 0.6410256 0.9000000                                        
6 0.3740458 0.6842105 0.4054054 0.6842105 0.2631579                              
7 0.4893617 0.8000000 0.5384615 0.8000000 0.2000000 0.2631579                    
8 0.4025974 0.8235294 0.6335878 0.8382353 0.4852941 0.4285714 0.4705882          
9 1.0000000 1.0000000 0.7297297 1.0000000 0.4736842 0.6666667 0.3684211 0.7936508

What I would like to extract from my distance object is the distances between each plot between each time step like this :

Plot    distance1   distance 2  distance3
1   0.5 0.75    0.9
2   0.1 0.2 0.3
3   0.01    0.1 0.5

where distance1 would be the distance from time 1 to 2, distance2 would be the distance from time 2 to 3 and distance3 would be the distance from time 1 to time 3.

Is there a relatively easy way of extracting this info from a distance object?


Solution

  • The easiest way to deal with this is probably to split the data by plot and then run vegdist on the three values for each plot.

    require(plyr)
    require(vegan)
    ddply(example, .(plot), function(x)as.vector(vegdist(x[,-1]))) 
    

    You probably should have a time column. You could use that to order the data to ensure that the first column of distances always represents the same times.