Last active
March 13, 2016 17:38
-
-
Save boopsboops/5718f05eaebe4284006c to your computer and use it in GitHub Desktop.
Modified function to generate interspecies distance matrices in R
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# load libs | |
require("spider") | |
#source(file="sppDistMatrix2.R")#can source the function | |
# load the example data | |
data(dolomedes) | |
doloDist <- dist.dna(dolomedes) | |
doloSpp <- substr(dimnames(dolomedes)[[1]], 1, 5) | |
# you have three options for dist, which are min, mean and max | |
sppDistMatrix2(distobj=doloDist, sppVector=doloSpp, dist=min) | |
# load the new function | |
sppDistMatrix2 <- function (distobj, sppVector, dist) | |
{ | |
dat <- as.matrix(distobj) | |
attr(dat, "dimnames")[[1]] <- sppVector | |
taxa <- unique(sppVector) | |
pair.mat <- matrix(data = NA, nrow = length(taxa), ncol = length(taxa), | |
dimnames = list(one = taxa, two = taxa)) | |
for (i in 1:length(taxa)) { | |
for (j in 1:length(taxa)) { | |
sppMat <- dat[which(dimnames(dat)[[1]] == taxa[i]), | |
which(dimnames(dat)[[1]] == taxa[j])] | |
if (taxa[i] == taxa[j]) | |
pair.mat[i, j] <- dist(sppMat[lower.tri(sppMat)]) | |
else pair.mat[i, j] <- dist(sppMat) | |
} | |
} | |
return(pair.mat) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment