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

Matt Strimas-Mackey strimas at zoology.ubc.ca
Fri Jun 12 19:10:29 CEST 2015


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!



More information about the R-sig-Geo mailing list