Donate. I desperately need donations to survive due to my health

Get paid by answering surveys Click here

Click here to donate

Remote/Work from Home jobs

Why does “as.matrix(dist object)” produce non-square matrices?

I try to loop around a large list of dataframes (n=1325) in R to calculate a diversity indicator for each dataframe (rows = sites, columns = species). The diversity indicator (beta) is defined by pairwise dissimilarities calculated using the betapart package. It produces a dist object (a dissimilarity matrix).

My problem:

The calculations work fine for single list elements, but when using the loop, I get the following warnings: In as.dist.default(beta.bray) : non-square matrix. This seems to be the result of NA values in beta. Why does as.matrix(dist object) produce non-square matrices?

Here is my code:

# single elements
pairwise <- beta.pair.abund(output[[1]], index.family="bray")
beta <- colMeans(as.matrix(pairwise$beta.bray))

# loop
for(i in 1:length(output)){
  pairwise <- beta.pair.abund(output[[i]], index.family="bray")
  beta <- colMeans(as.matrix(pairwise$beta.bray)) 
  output[[i]][,20] <- beta
}

Comments