0

I am trying to coerce a distance matrix from QIIME2 to class dist in R. Upstream processing up to the generation of the distance matrix was performed as described in the QIIME2 document "Moving Pictures". When I call as.dist, I get the following warning messages:

imported_dist_matrix <- as.dist(dist_bray_df)
## Warning messages:
## 1: In storage.mode(m) <- "numeric" : NAs introduced by coercion
## 2: In as.dist.default(dist_bray_df) : non-square matrix

imported_dist_matrix <- as.dist(dist_bray)
## Warning messages:
## 1: In storage.mode(m) <- "numeric" : NAs introduced by coercion
## 2: In as.dist.default(dist_bray) : non-square matrix

I do not know why the distance matrix is non-square:

print(dist_bray)
## A tibble: 130 × 131
##    ...1      1-12-…¹ 1-12-…² 1-12-…³ 1-8-3…⁴ 1-8-3…⁵ 1-8-3…⁶ 2-21-…⁷ 2-21-…⁸ 2-21-…⁹ 3-15-…˟ 3-15-…˟
##    <chr>       <dbl>   <dbl>   <dbl>   <dbl>   <dbl>   <dbl>   <dbl>   <dbl>   <dbl>   <dbl>   <dbl>
##  1 1-12-43-…   0       0.462   0.552   0.716   0.996   0.918   0.755   0.649   0.671   0.664   0.712

I want a dist object so that I can pass it to metaMDS, and so on.

Mikael Jagan
  • 9,012
  • 2
  • 17
  • 48
  • Welcome to Stack Overflow. Please take the [tour](https://stackoverflow.com/tour) and read this: https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example Then edit your question to supply additional information necessary to help you. – John Polo Mar 19 '23 at 01:46
  • Please provide enough code so others can better understand or reproduce the problem. – Community Mar 19 '23 at 07:39

1 Answers1

0

I solved my issue like this. In the end it all boiled down to basic R things like what is a tibble vs a matrix vs a dataframe. The solution was: set column names to TRUE and omit the row names

dist_bray <- read_tsv(file='distance-matrix-for-R.tsv', col_names=TRUE)
dist_bray[1] <- NULL
correct<-as.dist(dist_bray)
metaMDS(correct)

I'm just leaving this here in case anyone else will find this useful in the future.

Best Regards!