Skip to content

Instantly share code, notes, and snippets.

@vsbuffalo
Last active August 29, 2015 14:05
Show Gist options
  • Save vsbuffalo/ffa8084f600d3359b742 to your computer and use it in GitHub Desktop.
Save vsbuffalo/ffa8084f600d3359b742 to your computer and use it in GitHub Desktop.
# Rather trivial example of how dataframe row indexing could be used.
# I would not use this method, but it's not terrible design — access
# is fast compared to say genotype == 1L. Why limit these cases
> genotypes <- sample(0:2, 10, replace=TRUE)
> freq <- 0.3
> df <- data.frame(name=c("aa", "Aa", "AA"), type=c("homozygote", "heterozygote", "homozygote"),
freq=c((1-freq)^2, 2*freq*(1-freq), freq^2))
> df
name type freq
1 aa homozygote 0.49
2 Aa heterozygote 0.42
3 AA homozygote 0.09
> df$type[genotypes+1L]
[1] homozygote homozygote heterozygote homozygote heterozygote
[6] homozygote homozygote homozygote heterozygote heterozygote
Levels: heterozygote homozygote
> df$name[genotypes+1L]
[1] AA aa Aa aa Aa AA aa aa Aa Aa
Levels: aa Aa AA
> df$freq[genotypes+1L]
[1] 0.09 0.49 0.42 0.49 0.42 0.09 0.49 0.49 0.42 0.42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment