Skip to content

Instantly share code, notes, and snippets.

@dwinter
Last active February 19, 2016 04:12
Show Gist options
  • Save dwinter/addcf6693a1ee1e8b03a to your computer and use it in GitHub Desktop.
Save dwinter/addcf6693a1ee1e8b03a to your computer and use it in GitHub Desktop.
among-group distances
```r
inter_grp_dist <- function(combo, M, group_map){
vals <- M[group_map == combo[1], group_map == combo[2]]
list( combo=paste(combo, collapse="-"), mean=mean(vals), var=var(as.vector(vals)) )
}
within_grp_dist <- function(grp, M , group_map){
vals <- M[group_map == grp, group_map==grp]
vals <- vals[lower.tri(vals)]
list( combo=grp, mean=mean(vals), var=var(vals) )
}
group_dists <- function(dm, group_map){
between <- combn(unique(spp), 2, FUN=function(x) inter_grp_dist(x, dm, group_map), simplify=FALSE)
within <- lapply(unique(spp), within_grp_dist, dm, group_map)
do.call(rbind.data.frame, c(within, between))
}
group_dists(M, spp)
```
```sh
combo mean var
2 A 0.01359186 2.788514e-05
21 B 0.01359344 2.223939e-05
3 C 0.01178926 1.598179e-05
4 A-B 0.01412923 2.022738e-05
5 A-C 0.01310000 2.477668e-05
6 B-C 0.01229028 2.399985e-05
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment