[R-sig-Geo] [R] Point pattern to grid

Leaf Sun leaflovesun at yahoo.ca
Sun Nov 20 21:11:06 CET 2005


======= At 2005-11-18, 15:41:06 you wrote: =======

>On Fri, 18 Nov 2005, Leaf Sun wrote:
>
>> Hi Roger,
>> 
>> Thanks again for your kind help.
>> 
>> Yes, I still use the 200K points data applying this program but the good
>> thing is I found it finished in no time.
>
>I'm moving my reply to R-sig-geo, a more suitable list.
>
>> 
>> The questions again here are:
>> 
>> 1)   >try0 <- lapply(split(as(df1, "data.frame"), res), mean)  
>> 
>> When I tried to replace mean to sum, error looks like this:
>> 
>> Error in x at data[, i, drop = FALSE] : undefined columns selected 
>
>Please take a small subset of your data (say 25-50 points), and fake the 
>grid (say just 4 cells 250x250) to try to reach a function that seems 
>suitable. Then write your own function to give the output you need - look 
>carefully at ?split and ?lapply. Your function needs to return a single 
>vector (which can be a one-row data frame). The nafill values should suit 
>the ones you want to return. 
>
>> 
>> 2) If I just need to know the number of points in each cells, how can I
>> modify the codes. The codes still a bit beyond me.
>
>It might be length() or nrow() in your own function. If you try first with 
>a small example, and look at the results of each step in turn - so break:
>
>try0 <- lapply(split(as(df1, "data.frame"), res), mean)
>
>into
>
>try00 <- split(as(df1, "data.frame"), res)

Yes, I tracked the outcome of these two lines.

 try00 are always of 3 columns (x,y,z)  , but the strange thing is when I replace mean with  nrow, try0 turns out to be of only one column.

Thus ,  the final image is of one grid matrix X1 could be shown as:          image(sgrd1, "X1")     

>sgrd1 <- SpatialGridDataFrame(slot(sgrd, "grid"), try1)
> >image(sgrd1, "x")
> >image(sgrd1, "y")
> >image(sgrd1, "z")



>
>look what is inside a chosen member of the list try00, and try to work it 
>out from there. Once your function does what you want, run it on the full 
>data set, but still be careful in case impossible values have happened 
>(things like dividing by zero). Use summary() on the input point data to 
>get a good feel for the spread of the data. You're very close, but you 
>need to make these decisions in relation to your research problem, and how 
>you've operationalised your observed variables at the points (maybe you 
>need to store mean, sd, fivenum, and count for a numerical variable, but 
>only you can know).
>
>Roger
>
>
>> 
>> Thanks!
>> 
>> Leaf
>> 
>> ======= At 2005-11-18, 01:39:05 you wrote: =======
>> 
>> >On Thu, 17 Nov 2005, Leaf Sun wrote:
>> >
>> >> Dear all,
>> >> 
>> >> I'd like to change a point pattern to a grid of cells and use one of the
>> >> variables as the output.
>> >> 
>> >> e.g.  The point pattern is of a window of (500*500) and several features
>> >> such as pH, SoilType etc.  I like to divide it into a grid with cell
>> >> size 5*5, and use the mean of the point values falling inside the cell
>> >> as the output.
>> >> 
>> >> Is there any package in R working with this? Thanks in advance!
>> >
>> >This might have been better posted on R-sig-geo. Try this:
>> >
>> >library(sp)
>> >df1 <- data.frame(x=runif(10000,0,500), y=runif(10000,0,500),
>> >  z=rnorm(10000))
>> >coordinates(df1) <- c("x", "y")
>> >summary(df1) # SpatialPointsDataFrame
>> >grd <- GridTopology(c(2.5,2.5), c(5,5), c(100,100))
>> >sgrd <- SpatialGrid(grd) #SpatialGrid
>> >bbox(sgrd)
>> >res <- overlay(sgrd, df1)
>> ># find which grid cells the points are in
>> >str(res)
>> >try0 <- lapply(split(as(df1, "data.frame"), res), mean)
>> ># take means by grid cell - assumes all numeric columns in df1
>> ># (soil type??) - maybe write a custom function to handle non-numeric 
>> ># columns sensibly
>> >try01 <- vector(mode="list", length=prod(slot(slot(sgrd, "grid"),
>> >  "cells.dim")))
>> >nafill <- rep(as.numeric(NA), ncol(as(df1, "data.frame")))
>> >try01 <- lapply(try01, function(x) nafill)
>> ># make a container to put the means in with the right number of columns
>> >try01[as.integer(names(try0))] <- try0
>> ># insert means into correct list elements
>> >try1 <- data.frame(t(data.frame(try01)))
>> ># transpose
>> >summary(try1)
>> >sgrd1 <- SpatialGridDataFrame(slot(sgrd, "grid"), try1)
>> >image(sgrd1, "x")
>> >image(sgrd1, "y")
>> >image(sgrd1, "z")
>> >
>> >It goes a bit further than the short description of the sp package in the 
>> >latest R-News, and will most likely be a new method for overlay in sp. If 
>> >these are your 200K points, it may take a little longer ...
>> >
>> >> 
>> >> Cheers,
>> >> 
>> >> Leaf
>> >> 
>> >> 
>> >
>> >-- 
>> >Roger Bivand
>> >Economic Geography Section, Department of Economics, Norwegian School of
>> >Economics and Business Administration, Helleveien 30, N-5045 Bergen,
>> >Norway. voice: +47 55 95 93 55; fax +47 55 95 95 43
>> >e-mail: Roger.Bivand at nhh.no
>> >
>> 
>> = = = = = = = = = = = = = = = = = = = =
>> 
>> 
>
>-- 
>Roger Bivand
>Economic Geography Section, Department of Economics, Norwegian School of
>Economics and Business Administration, Helleveien 30, N-5045 Bergen,
>Norway. voice: +47 55 95 93 55; fax +47 55 95 95 43
>e-mail: Roger.Bivand at nhh.no
>
>

= = = = = = = = = = = = = = = = = = = =




More information about the R-sig-Geo mailing list