Thanks, Andy. I made a small modification to the code to make it give
the number of sites at which two species co-occur. (This just involves
changing "any" to "sum".)
spp.cooc.count <- function(matrx) {
# first we make a list of all the sites where each spp is found
site.list <- apply(matrx,2,function(x) which(x > 0))
# then we see which spp are found at the same sites
sapply(site.list,function(x1)
{
sapply(site.list,function(x2) 1*sum(x2 %in% x1))
})
# the result is returned in a symmetrical matrix of dimension
# equal to the number of spp
}
Jane
On Fri, Aug 27, 2010 at 9:41 AM, Andy Rominger <[email protected]> wrote:
> Hi Jane,
>
> I think someone may have asked something similar on the r-sig-eco email list
> (which is a good resource in general:
> https://stat.ethz.ch/mailman/listinfo/r-sig-ecology)
>
> I think the answer may have been there there's a function in the vegan
> package for R (http://cran.r-project.org/web/packages/vegan/index.html).
>
> But it would be pretty simple to write something up in R. Here's one way of
> doing it (if I'm correct in my interpretation of a co-occurrence matrix!).
> The actual function (called `spp.cooc') is really only 2 lines long--the
> code just looks longer from making up example data and adding in the
> comments.
>
> Hope this might do the trick for you! Note that in it's current form you
> would have to give the function a matrix or data.frame of ONLY NUMBERS in
> which species are columns and sites are rows. This could be changed by
> manipulating the MARGIN argument of the apply command below, i.e., site.list
> <- apply(matrx,1,...)
>
> Hope this helps--
> Andy
>
>
> # make some example data
> sppXsite <- matrix(rpois(15,0.5),nrow=3)
> colnames(sppXsite) <- paste("spp",1:5,sep="")
> rownames(sppXsite) <- paste("site",1:3,sep="")
> sppXsite # here's what it looks like
>
> # now make a function to compute the co-occurrence matrix
> spp.cooc <- function(matrx) {
> # first we make a list of all the sites where each spp is found
> site.list <- apply(matrx,2,function(x) which(x > 0))
> # then we see which spp are found at the same sites
> sapply(site.list,function(x1)
> {
> sapply(site.list,function(x2) 1*any(x2 %in% x1))
> })
> # the result is returned in a symmetrical matrix of dimension
> # equal to the number of spp
> }
>
> # here's how it works
> co.matrix <- spp.cooc(sppXsite)
> co.matrix
>
>
>
>
> On Fri, Aug 27, 2010 at 12:46 AM, Jane Shevtsov <[email protected]> wrote:
>>
>> Is there a fast way to make a species co-occurrence matrix given a
>> site-species matrix or lists of species found at each site? I'm
>> looking for a spreadsheet or database method (preferably OpenOffice)
>> or R function.
>>
>> Thanks,
>> Jane
>>
>> --
>> -------------
>> Jane Shevtsov
>> Ecology Ph.D. candidate, University of Georgia
>> co-founder, <www.worldbeyondborders.org>
>> Check out my blog, <http://perceivingwholes.blogspot.com>Perceiving Wholes
>>
>> "The whole person must have both the humility to nurture the
>> Earth and the pride to go to Mars." --Wyn Wachhorst, The Dream
>> of Spaceflight
>
>
--
-------------
Jane Shevtsov
Ecology Ph.D. candidate, University of Georgia
co-founder, <www.worldbeyondborders.org>
Check out my blog, <http://perceivingwholes.blogspot.com>Perceiving Wholes
"The whole person must have both the humility to nurture the
Earth and the pride to go to Mars." --Wyn Wachhorst, The Dream
of Spaceflight