Created
February 8, 2016 06:55
-
-
Save anpefi/10d61508a0355e416482 to your computer and use it in GitHub Desktop.
This script obtains the distribution if Sannon's Index of diversity for all the AFLP loci
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
# Assuming you have your aflp matrix data with samples in rows and | |
# loci in columns. The file is a csv file with the format of msap | |
#Read data | |
data <- read.csv("aflp.csv", header=TRUE) | |
#convert to matrix | |
matN <- as.matrix(data[,4:length(data[1,])]) | |
#Function for sannon index | |
shannon <-function(x){ | |
#Calculate Shannon's Diversity Index given a list of binomial alleles | |
t <- table(x) | |
if (length(t)<2) return(NaN) | |
P=t[1]/sum(t) | |
Q=t[2]/sum(t) | |
S=-(P*log(P)+Q*log(Q)) | |
return(S) | |
} | |
#Apply the funtion to your data | |
Sannon.I <- apply(matM, 2, shannon) | |
summary(Sannon.I) | |
hist(Sannon.I) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment