R code for community dynamics project