[R-sig-eco] reorder vegdist objects

Bede-Fazekas Ákos b|@|ev||@t @end|ng |rom gm@||@com
Sat Jun 1 06:47:02 CEST 2019


Dear Gian,
Let's do a small reproducible example:
library(vegan)
data(varespec)
set.seed(12345)
df_a <- t(varespec)
df_b <- t(varespec[, sample.int(ncol(varespec))]) # shuffle the original 
column order
dist_a <- vegdist(df_a); dist_b <- vegdist(df_b)
all.equal(dist_a, dist_b, check.attributes = FALSE)

Now, you have two options. If you have access to the original 
data.frame/matrix from which the dist object was created, you can 
reorder the rows of the second dataset to match that of the first one. 
Then do the distance matrix calculation again.
df_b_reordered <- df_b[rownames(df_a), ]
dist_b_reordered1 <- vegdist(df_b_reordered)
all.equal(dist_a, dist_b_reordered1, check.attributes = FALSE)

If you have no access to the original dataset and/or can not recalculate 
the distance matrix (e.g. due to computer capacity or time), you can 
reorder the distance matrix (both the rows and columns).
dist_b_reordered2 <- as.dist(as.matrix(dist_b)[attr(dist_a, "Labels"), 
attr(dist_a, "Labels")])
all.equal(dist_a, dist_b_reordered2, check.attributes = FALSE)

HTH,
Ákos Bede-Fazekas
Hungarian Academy of Sciences


2019.06.01. 1:53 keltezéssel, Gian Maria Niccolò Benucci írta:
> Hello,
>
> I have two vegan::vegdist objects I want to use into a vegan::mantel test.
> However, rows and columns of the two objects are not in the same order. Is
> there a way to reorder them one another?
>
> For example,
>
>> dist_a
>          Lycovolu  Hupeaust  Lycofast  Lycodeut
> Hupeaust 0.9045557
>
> Lycofast 0.6835441 0.8569781
>
> Lycodeut 0.8036577 0.8294657 0.7958988   ...
> ...
>
>> dist_b
>           Lycodeut   Lycofast   Lycoscar   Lycovolu
> Lycofast 0.09340659
>
> Lycoscar 0.08974359 0.01831502
>
> Lycovolu 0.09073476 0.01099655 0.01008080   ...
> ...
>
> Thanks,
>



More information about the R-sig-ecology mailing list