Hi Robert

Thanks very much for your reply and the code you provide. When I run the
code, I could not get the rows to give me the IDs of the grid cells.

In my case, I have two sets of data.

The first set are polygons of several species distributions merged together
with the attribute of the merged polygon representing the species names.

The second set are the grid cells prepared in ArcMap and they are of fixed
size, 50 by 50 km. Each grid has an ID and longitude/latitude locality. My
aim is to know which species is present in each grid, thereby creating a
presence/absence matrix where the columns are the names of the species in
the merged file, and rows are the IDs of the grid cells.

If it is OK by you, I can send you a subset of the data as shapefiles to
see what I mean.

Thanks and kind regards

Barnabas Daru


On Thu, Jan 23, 2014 at 8:51 PM, Robert J. Hijmans <r.hijmans@gmail.com>wrote:

> Here is an approach:
>
> library(raster)
> p1 <- rbind(c(-180,-20), c(-140,55), c(10, 0), c(-140,-60), c(-180,-20))
> hole <- rbind(c(-150,-20), c(-100,-10), c(-110,20), c(-150,-20))
> sp1 <- SpatialPolygons(list(Polygons(list(Polygon(p1), Polygon(hole,
> hole=TRUE)), 1)))
> sp2 <- SpatialPolygons(list(Polygons(list(Polygon(rbind(c(-10,0),
> c(140,60), c(160,0), c(140,-55), c(-10,0)))), 2)))
> sp3 <- SpatialPolygons(list(Polygons(list(Polygon(rbind(c(-125,0),
> c(0,60), c(40,5), c(15,-45), c(-125,0)))), 3)))
>
> species <- list(sp1, sp2, sp3)
> r <- raster(ncol=90, nrow=45)
>
> rasters <- list()
> for (i in 1:length(species)) {
>    rasters[[i]] <- rasterize(species[[i]], r, field=1, background=0)
> }
> s <- stack(rasters)
> x <- as.data.frame(s)
>
> # alternatively, look at 'over' in sp.
>
> ################
>
> # And this is how you might do it with shapefiles from a single directory
> sps = list.files(pattern='.shp$')
> # adjust raster to your taste (extent, resolution)
> r <- raster(ncol=90, nrow=45)
>
> rasters <- list()
> for (i in 1:length(sps)) {
>    sp <- shapefile(sps[i])
>    f <- extension(sp, '.grd')
>    rasters[[i]] <- rasterize(species[[i]], r, field=1, background=0,
> filename=f, overwrite=TRUE)
> }
> s <- stack(rasters)
> x <- as.data.frame(s)
>
>
> On Thu, Jan 23, 2014 at 3:53 AM, Barnabas Daru <darunabas@gmail.com>
> wrote:
> > Dear list members,
> > I will like to create a presence/absence matrix using R where rows
> represent grid cells and columns species.
> >
> > I have a set of overlapping polygons each representing the distribution
> of different species and grid cells (also shapefiles generated using
> fishnet tool in ArcMap) to show presence or absence of species within the
> grid cells.
> >
> > In Arcmap, I was able to do this successfully for few species as follows:
> > (1) I merged the polygons
> > (2) I performed spatial intersect of the merged maps on the grid cells
> > (3) From the result, I used the dataframe (dbf) of the intersect to
> create the presence absence matrix using the function sample2matrix in the
> R package picante. The 3 columns of the dataframe are:
> > plots | abundance | species
> >
> > However, given the number of merged maps (over 200 overlapping
> polygons), the analysis in Arcmap got stalled and never proceed to
> completion.
> > I will therefore like to do this in R.
> > I will greatly appreciate any R code to help me do this.
> >
> > Thanks and kind regards
> > Barnabas
> >
> > _______________________________________________
> > R-sig-Geo mailing list
> > R-sig-Geo@r-project.org
> > https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>



-- 

  \-/
   /\
  /--|
 /---/ *Daru, Barnabas Haruna*
 |--/  PhD Candidate,
 \-/   African Centre for DNA Barcoding,
 /\    University of Johannesburg,
/--\   PO BOX 524 Auckland Park, 2006,
|---\  South Africa
 \---\ Lab: +27 11 559 3477
  \--| Mobile: +27 7381 89 583
   \-/
   /\  My homepage:
http://barnabasdaru.com<http://acdb.co.za/index.php?page=mr-barnabas-h-daru>

  /--\

	[[alternative HTML version deleted]]

