[R-sig-Geo] Extract values with over()

Edzer Pebesma edzer.pebesma at uni-muenster.de
Tue Nov 22 14:36:12 CET 2011



On 11/21/2011 09:24 PM, Paulo Justiniano Ribeiro Jr wrote:
> Dear Edzer and Alexandre
> 
> wouldn't the following work as requested?
> 
> fullgrid(grid) <- FALSE
> class(grid)  ## SpatialPixels
> 
> ap3 <- as.SpatialPolygons.SpatialPixels(grid)
> table(over(pontos, ap3))

Paulo, that is a step in the right direction, as

> table(over(pontos, ap3))[1:5]

1 2 3 4 5
2 1 3 1 2

gives frequencies for each cell, but this would also be obtained by the
grid itself:

> table(over(pontos, grid))[1:5]

1 2 3 4 5
2 1 3 1 2

However, as

> length(table(over(pontos, grid)))
[1] 298
> length(grid)
[1] 400

shows we have empty grid cells. but this gives gaps (implicit 0 values)
for empty cells in grid. The simpler alternative is to do the over with
returnList=TRUE, which returns ALL points in each (grid cell, now
polygon) in ap3:

>  over(ap3, pontos, returnList=TRUE)[1:5]
[[1]]
[1] 416 459

[[2]]
[1] 385

[[3]]
[1]  17 132 351

[[4]]
[1] 387

[[5]]
[1] 311 322

taking the length of each vector gives the frequencies (INCLUDING the
zero frequencies that table does NOT give you):

n = sapply(over(ap3, pontos, returnList=TRUE), length)
x = SpatialPixelsDataFrame(grid, data.frame(n=n))
spplot(x, sp.layout=list("sp.points", pontos), at=0:6-.5)

Without doubt, there are other ways of doing this, for instance with
package raster.

Thanks for helping. It seems to be hard to make complex things simple,
and I'd be happy to hear suggestions for making this even simpler.

> 
> 
> 
> Paulo Justiniano Ribeiro Jr
> LEG (Laboratorio de Estatistica e Geoinformacao)
> Universidade Federal do Parana
> Caixa Postal 19.081
> CEP 81.531-990
> Curitiba, PR  -  Brasil
> Tel: (+55) 41 3361 3573
> VOIP: (+55) (41) (3361 3600) 1053 1066
> Fax: (+55) 41 3361 3141
> e-mail: paulojus AT  ufpr  br
> http://www.leg.ufpr.br/~paulojus
> 
> On Mon, 21 Nov 2011, Edzer Pebesma wrote:
> 
>>
>>
>> On 11/21/2011 08:47 PM, ASANTOS wrote:
>>> Dear colleges,
>>>
>>>      I try to extract values with over () function , but not working
>>> with my script, when I make:
>>>
>>
>> it only extracts values when there are values to extract, please read
>> the documentation.
>>
>>>
>>> was expected that the over() give the frequency of points inside each
>>> parcel
>>
>> I wonder where you got this expectation from. If the documentation gave
>> you this expectation, please let me know.
>>
>> table() can compute frequencies from indices.
>> -- 
>> Edzer Pebesma
>> Institute for Geoinformatics (ifgi), University of Münster
>> Weseler Straße 253, 48151 Münster, Germany. Phone: +49 251
>> 8333081, Fax: +49 251 8339763  http://ifgi.uni-muenster.de
>> http://www.52north.org/geostatistics      e.pebesma at wwu.de
>>
>> _______________________________________________
>> R-sig-Geo mailing list
>> R-sig-Geo at r-project.org
>> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>>

-- 
Edzer Pebesma
Institute for Geoinformatics (ifgi), University of Münster
Weseler Straße 253, 48151 Münster, Germany. Phone: +49 251
8333081, Fax: +49 251 8339763  http://ifgi.uni-muenster.de
http://www.52north.org/geostatistics      e.pebesma at wwu.de



More information about the R-sig-Geo mailing list