[R-sig-Geo] group data into squares
White.Denis at epamail.epa.gov
White.Denis at epamail.epa.gov
Wed Nov 29 19:20:19 CET 2006
Luis Ridao Cruz <Luisr at frs.fo> wrote on 2006-11-29 03:40:25:
> R-sig-geo-help,
>
> I have locations of fish abundance
> which I want to aggregate into squares.
>
> The code below creates the grid:
>
>
> library(sp)
> test <- GridTopology(c(-10.5,60),c(0.1,0.1),c(67,35))
> coordinates(test)
> coordinatevalues(test)
> yy <-SpatialGrid(grid=test)
> plot(yy,pch=16,cex=0.3,col=1)
>
> and within each cell the total abundance of fish
> should be specified.
>
> How can I do this?
>
Here's an old fashioned way:
# set up grid
nx <- 67
ny <- 35
test <- matrix (0, nrow=ny, ncol=nx)
xmin <- -10.5
ymin <- 60
xinc <- 0.1
yinc <- 0.1
# generate random sample points
npts <- 5000
pts <- cbind (x = runif (npts) * nx * xinc + xmin,
y = runif (npts) * ny * yinc + ymin)
# count points in grid cells
for (i in 1:npts)
{
x <- pts[i, 1]
y <- pts[i, 2]
tcol <- round ((x - xmin) / xinc)
trow <- round ((y - ymin) / yinc)
test[trow, tcol] <- test[trow, tcol] + 1
}
# distribution of counts
table (test)
sum (table (test))
More information about the R-sig-Geo
mailing list