[R-sig-Geo] Error in sp::aggregate() when raster package is loaded

Edzer Pebesma edzer.pebesma at uni-muenster.de
Fri Jun 12 21:43:04 CEST 2015


Thanks for bringing this up, Matt. I appreciate your creative solution,
but this of course drives ordinary users nuts.

Package raster (which is about "Geographic Data Analysis and Modeling")
is moving into areas that have nothing to do with rasters. Robert and I
need to sort this out.

On 06/12/2015 07:10 PM, Matt Strimas-Mackey wrote:
> It seems I posted this too soon and I've answered my own question.
> Obviously, raster has its own aggregate function, and I now realize
> the error is occurring because raster::aggregate was being called
> rather than sp::aggregate. Oddly, when I tried sp::aggregate() or
> sp:::aggregate(), I get
> Error: 'aggregate' is not an exported object from 'namespace:sp'
> and
> Error in get(name, envir = asNamespace(pkg), inherits = FALSE) :
>   object 'aggregate' not found
> respectively. However, I finally managed to fix this using:
> 
> spAgg <- get('aggregate', envir=as.environment("package:sp"))
> ret2 <- spAgg(p1["x"], p2, areaWeighted = TRUE)
> 
> Apologies for posting unnecessarily!
> 
> On Fri, Jun 12, 2015 at 9:23 AM, Matt Strimas-Mackey
> <strimas at zoology.ubc.ca> wrote:
>> I'm trying to run sp::aggregate() with SpatialPolygons objects.
>> Everything works as expected, unless the raster package is loaded in
>> which case I get the following error:
>>
>> Error in unique.default(v) : unique() applies only to vectors
>>
>> I suppose I could unload and reload raster, but ideally I'd like to
>> avoid this. I'm running this on a Mac in RStudio with R version: 3.2.0
>> (2015-04-16) -- "Full of Ingredients". All packages are up to date.
>>
>> Here's a example from the help for aggregate(), which reproduces the error:
>>
>>
>> library(sp)
>> library(raster) # Error unless this line is commented out
>>
>> # Area-weighted example, using two partly overlapping grids:
>>
>> gt1 = SpatialGrid(GridTopology(c(0,0), c(1,1), c(4,4)))
>> gt2 = SpatialGrid(GridTopology(c(-1.25,-1.25), c(1,1), c(4,4)))
>>
>> # convert both to polygons; give p1 attributes to aggregate
>> p1 = SpatialPolygonsDataFrame(as(gt1, "SpatialPolygons"),
>>                               data.frame(v = 1:16, w=5:20,
>> x=factor(1:16)), match.ID = FALSE)
>> p2 = as(gt2, "SpatialPolygons")
>>
>> # all-factor attributes: compute area-dominant factor level:
>> ret = aggregate(p1["x"], p2, areaWeighted = TRUE)
>> # Error in unique.default(v) : unique() applies only to vectors
>>
>>
>> Thanks!
> 
> _______________________________________________
> 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,
Heisenbergstraße 2, 48149 Münster, Germany; +49 251 83 33081
Journal of Statistical Software:   http://www.jstatsoft.org/
Computers & Geosciences:   http://elsevier.com/locate/cageo/
Spatial Statistics Society http://www.spatialstatistics.info

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 490 bytes
Desc: OpenPGP digital signature
URL: <https://stat.ethz.ch/pipermail/r-sig-geo/attachments/20150612/fd41b252/attachment.bin>


More information about the R-sig-Geo mailing list