Dear Happy R-users & experts, I am in need of advice, While working with spatial data (x & y coordinates of seed locations) I have come accross the problem that I need to convert my point data into a matrix or grid system. I then need to count how often a point falls into a certain position in the matrix or grid. I have searched all day online, asked collegeas but nothing works. Sadly my R "box of tricks" has run out. My (point) data looks like this; x y 2.3 4.5 3.4 0.2 and continues for another million records. Now my question; is there any function that is able to "count" how often a point falls into a grid based on the x and y location? So I need to discretize the spatial locations to a regular grid and then counting how often a point occurs. Many thanks for your thoughts on this problem. Marco Visser __________________________________________________ [[alternative HTML version deleted]]
On Wed, 18 Apr 2007, Marco Visser wrote:> Dear Happy R-users & experts, > > I am in need of advice, > While working with spatial data (x & y coordinates of seed locations) I have come accross the problem that I need to convert my point data into a matrix or grid system. I then need to count how often a point falls into a certain position in the matrix or grid. I have searched all day online, asked collegeas but nothing works. > > Sadly my R "box of tricks" has run out. > > My (point) data looks like this; > > x y > 2.3 4.5 > 3.4 0.2 > > and continues for another million records. > > Now my question; is there any function that is able to "count" how often > a point falls into a grid based on the x and y location? So I need to > discretize the spatial locations to a regular grid and then counting how > often a point occurs.see ?table and ?cut Maybe something like x.breakpoints <- <sensible breakpoints for x> y.breakpoints <- <sensible breakpoints for y> my.grid <- table( cut( x, x.breakpoints ), cut( y, y.breakpoints ) ) see also ?xtab and ?quantile> > Many thanks for your thoughts on this problem. > > Marco Visser > > > > > > > > > > __________________________________________________ > > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >Charles C. Berry (858) 534-2098 Dept of Family/Preventive Medicine E mailto:cberry at tajo.ucsd.edu UC San Diego http://biostat.ucsd.edu/~cberry/ La Jolla, San Diego 92093-0901
--- Marco Visser <visser_md at yahoo.com> wrote:> Dear Happy R-users & experts, > > I am in need of advice, > While working with spatial data (x & y coordinates > of seed locations) I have come accross the problem > that I need to convert my point data into a matrix > or grid system. I then need to count how often a > point falls into a certain position in the matrix or > grid. I have searched all day online, asked > collegeas but nothing works. > > Sadly my R "box of tricks" has run out. > > My (point) data looks like this; > > x y > 2.3 4.5 > 3.4 0.2 > > and continues for another million records. > > Now my question; is there any function that is able > to "count" how often a point falls into a grid based > on the x and y location? So I need to discretize the > spatial locations to a regular grid and then > counting how often a point occurs. > > Many thanks for your thoughts on this problem. > > Marco VisserWould something like ?crossprod do it?> > > > > > > > > > __________________________________________________ > > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, > reproducible code. >
Marco, I've done something similar with spatial data. I defined the points as SpatialPoints, the grid as SpatialGrid (using the "sp" package). Then table(overlay(grid, points)) will give you the number of points inside each gridcell. library("sp") points <- SpatialPoints(your.data.frame) cellsize <- 1 cellcentre.offset <- bbox(points)[, 1] coords.span <- diff(t(bbox(points))) cells.dim <- ceiling(coords.span / cellsize) grid <- SpatialGrid(GridTopology(cellcentre.offset, rep(cellsize, nrow(bbox(points))), cells.dim)) in.cell <- overlay(grid, points) table(in.cell) Cheers, Thierry PS R-sig-geo is a better list to ask spatial releated questions. ------------------------------------------------------------------------ ---- ir. Thierry Onkelinx Instituut voor natuur- en bosonderzoek / Reseach Institute for Nature and Forest Cel biometrie, methodologie en kwaliteitszorg / Section biometrics, methodology and quality assurance Gaverstraat 4 9500 Geraardsbergen Belgium tel. + 32 54/436 185 Thierry.Onkelinx op inbo.be www.inbo.be Do not put your faith in what statistics say until you have carefully considered what they do not say. ~William W. Watt A statistical analysis, properly conducted, is a delicate dissection of uncertainties, a surgery of suppositions. ~M.J.Moroney> -----Oorspronkelijk bericht----- > Van: r-help-bounces op stat.math.ethz.ch > [mailto:r-help-bounces op stat.math.ethz.ch] Namens Marco Visser > Verzonden: woensdag 18 april 2007 20:57 > Aan: r-help op stat.math.ethz.ch > Onderwerp: [R] Matrix or grid conversion of spatial data > > Dear Happy R-users & experts, > > I am in need of advice, > While working with spatial data (x & y coordinates of seed > locations) I have come accross the problem that I need to > convert my point data into a matrix or grid system. I then > need to count how often a point falls into a certain position > in the matrix or grid. I have searched all day online, asked > collegeas but nothing works. > > Sadly my R "box of tricks" has run out. > > My (point) data looks like this; > > x y > 2.3 4.5 > 3.4 0.2 > > and continues for another million records. > > Now my question; is there any function that is able to > "count" how often a point falls into a grid based on the x > and y location? So I need to discretize the spatial locations > to a regular grid and then counting how often a point occurs. > > Many thanks for your thoughts on this problem. > > Marco Visser > > > > > > > > > > __________________________________________________ > > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help op stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >