findzeros <- function(d, both=F, names=T) { # find the pairs of sites with 0 distance in a distance matrix # input: distance object (e.g. from dist() or vegdist() # output: matrix of pairs of sites with dist=0 # default is only unique pairs, but both=T gives both # return site names if names=T otherwise return indices dm <- as.matrix(d) rm <- row(dm)[dm==0] cm <- col(dm)[dm==0] # keep non-diagonals (both=T) or upper triangle (if both=F) if (both) { keep <- rm != cm} else {keep <- rm < cm } # ifelse() doesn't seem to work here sites <- dimnames(dm)[[1]] if (names) {cbind(row=sites[rm[keep]],col=sites[cm[keep]]) } else {cbind(row=rm[keep],col=cm[keep]) } }