X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=R%2Fdist.gene.R;h=516812a94e1d617088003f688c1670f458ec5553;hb=2653eb671caf9234635e44b895ef48b377a89a78;hp=ea17b9b577a37c3cc617d552d9e5bfa5d8eb3465;hpb=f5788af1ae347b00a14d94d12b50b3804d63e9bf;p=ape.git diff --git a/R/dist.gene.R b/R/dist.gene.R index ea17b9b..516812a 100644 --- a/R/dist.gene.R +++ b/R/dist.gene.R @@ -1,8 +1,8 @@ -## dist.gene.R (2008-07-18) +## dist.gene.R (2012-04-02) ## Pairwise Distances from Genetic Data -## Copyright 2002-2008 Emmanuel Paradis +## Copyright 2002-2012 Emmanuel Paradis ## This file is part of the R-package `ape'. ## See the file ../COPYING for licensing issues. @@ -11,27 +11,29 @@ dist.gene <- function(x, method = "pairwise", pairwise.deletion = FALSE, variance = FALSE) { - if (!is.data.frame(x) || !is.matrix(x)) - stop("'x' should be a matrix or a data.frame") + if (is.data.frame(x)) x <- as.matrix(x) else { # suggestion by Markus Schlegel + if (!is.matrix(x)) + stop("'x' should be a matrix or a data.frame") + } method <- match.arg(method, c("pairwise", "percentage")) if (!pairwise.deletion) { ## delete the columns with at least one NA: del <- apply(x, 2, function(xx) any(is.na(xx))) - x <- x[, -del] + x <- x[, !del] } n <- dim(x) L <- n[2] n <- n[1] - D <- double(n*(n - 1)/2) + D <- double(n * (n - 1)/2) if (pairwise.deletion) L <- D - k <- 1 + k <- 1L for (i in 1:(n - 1)) { for (j in (i + 1):n) { y <- x[i, ] != x[j, ] if (pairwise.deletion) L[k] <- sum(!is.na(y)) D[k] <- sum(y, na.rm = TRUE) - k <- k + 1 + k <- k + 1L } } ## L is either a single integer value if pairwise.deletion = FALSE, @@ -41,14 +43,14 @@ dist.gene <- attr(D, "Size") <- n attr(D, "Labels") <- dimnames(x)[[1]] - attr(D, "Diag") <- attr(d, "Upper") <- FALSE + attr(D, "Diag") <- attr(D, "Upper") <- FALSE attr(D, "call") <- match.call() attr(D, "method") <- method class(D) <- "dist" if (variance) { y <- if (method == "pairwise") L else 1 - attr(D, "variance") <- D*(y - D)/L + attr(D, "variance") <- D * (y - D)/L } D }