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

Matt Strimas-Mackey strimas at zoology.ubc.ca
Fri Jun 12 18:23:38 CEST 2015


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