1 ## dist.gene.R (2009-04-01)
3 ## Pairwise Distances from Genetic Data
5 ## Copyright 2002-2009 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) && !is.matrix(x))
15 stop("'x' should be a matrix or a data.frame")
16 method <- match.arg(method, c("pairwise", "percentage"))
18 if (!pairwise.deletion) {
19 ## delete the columns with at least one NA:
20 del <- apply(x, 2, function(xx) any(is.na(xx)))
26 D <- double(n*(n - 1)/2)
27 if (pairwise.deletion) L <- D
29 for (i in 1:(n - 1)) {
30 for (j in (i + 1):n) {
32 if (pairwise.deletion) L[k] <- sum(!is.na(y))
33 D[k] <- sum(y, na.rm = TRUE)
37 ## L is either a single integer value if pairwise.deletion = FALSE,
38 ## or a vector of integers if pairwise.deletion = TRUE
40 if (method == "percentage") D <- D/L
43 attr(D, "Labels") <- dimnames(x)[[1]]
44 attr(D, "Diag") <- attr(D, "Upper") <- FALSE
45 attr(D, "call") <- match.call()
46 attr(D, "method") <- method
50 y <- if (method == "pairwise") L else 1
51 attr(D, "variance") <- D*(y - D)/L