1 ## dist.gene.R (2012-04-02)
3 ## Pairwise Distances from Genetic Data
5 ## Copyright 2002-2012 Emmanuel Paradis
7 ## This file is part of the R-package `ape'.
8 ## See the file ../COPYING for licensing issues.
11 function(x, method = "pairwise", pairwise.deletion = FALSE,
14 if (is.data.frame(x)) x <- as.matrix(x) else { # suggestion by Markus Schlegel
16 stop("'x' should be a matrix or a data.frame")
18 method <- match.arg(method, c("pairwise", "percentage"))
20 if (!pairwise.deletion) {
21 ## delete the columns with at least one NA:
22 del <- apply(x, 2, function(xx) any(is.na(xx)))
28 D <- double(n * (n - 1)/2)
29 if (pairwise.deletion) L <- D
31 for (i in 1:(n - 1)) {
32 for (j in (i + 1):n) {
34 if (pairwise.deletion) L[k] <- sum(!is.na(y))
35 D[k] <- sum(y, na.rm = TRUE)
39 ## L is either a single integer value if pairwise.deletion = FALSE,
40 ## or a vector of integers if pairwise.deletion = TRUE
42 if (method == "percentage") D <- D/L
45 attr(D, "Labels") <- dimnames(x)[[1]]
46 attr(D, "Diag") <- attr(D, "Upper") <- FALSE
47 attr(D, "call") <- match.call()
48 attr(D, "method") <- method
52 y <- if (method == "pairwise") L else 1
53 attr(D, "variance") <- D * (y - D)/L