Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save DrK-Lo/0685e72775d44c7180bae99d3b99dc1c to your computer and use it in GitHub Desktop.
Save DrK-Lo/0685e72775d44c7180bae99d3b99dc1c to your computer and use it in GitHub Desktop.
#The genotype matrix has individuals in rows and mutations #in columns, with a 0,1, or 2 entered for the number of #alternate alleles in the diploid
# Let’s create a matrix with 5 individuals, 10 SNPS, and some missing data:
set.seed(345)
GEN = matrix(sample(0:2, 50, replace=TRUE), nrow=5, ncol=10)
GEN[5,1] <- NA
GEN
# Function to calculate the allele frequency of a column
af <- function(i, gen){
sum(gen[,i], na.rm=TRUE)/(2*sum(!is.na(gen[,i])))
}
af(1, GEN)
# Code to calculate the allele frequencies of all SNPs in the GEN matrix
apply(GEN, 2, function(i){
sum(i, na.rm=TRUE)/(2*sum(!is.na(i)))})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment