在R不兼容类型中替换空间多边形数据框中的值

在R不兼容类型中替换空间多边形数据框中的值,r,aggregate-functions,spatial,sp,R,Aggregate Functions,Spatial,Sp,我正在使用R中的sp包对多边形中的点属性进行相当简单的聚合(即SpatialPointsDataFrame中的SpatialPointsDataFrame)。我相信我可以毫无问题地做到这一点。但是,为了绘制“更漂亮”的图,我想用数字0替换没有出现点的聚集区域(使用aggregate函数后给定NAs)。执行此操作时,我收到错误: [使用countyAgg[naIndex,“Att”]中出现错误谢谢!我知道这很简单。我非常感谢您的帮助! library(sp) library(raster) ##

我正在使用R中的
sp
包对多边形中的点属性进行相当简单的聚合(即
SpatialPointsDataFrame
中的
SpatialPointsDataFrame
)。我相信我可以毫无问题地做到这一点。但是,为了绘制“更漂亮”的图,我想用数字0替换没有出现点的聚集区域(使用
aggregate
函数后给定NAs)。执行此操作时,我收到错误:


[使用
countyAgg[naIndex,“Att”]中出现错误谢谢!我知道这很简单。我非常感谢您的帮助!
library(sp)
library(raster)

### Get Washington County map ###
USCounty <- raster::getData('GADM', country='USA', level=2) # Download US County Map
WA <- USCounty[USCounty$NAME_1 == "Washington",] # Subset data to just the State of WA

### Create point data ###

# Set parameters
numPts <- 30 # Set the number of points

# Set projection I want
ptsCRS <- "+proj=aea +lat_1=29.5 +lat_2=45.5 +lat_0=23 +lon_0=-96 +x_0=0 +y_0=0 +datum=NAD83 +units=m +no_defs +ellps=GRS80 +towgs84=0,0,0"

# Project data
WA <- spTransform(WA, ptsCRS)

# Create point coordinates and attribute data
set.seed(22)
x <- sample(xmin(WA):xmax(WA), numPts, replace = TRUE) #xvalues in the extent of the state
y <- sample(ymin(WA):ymax(WA), numPts, replace = TRUE) #yvalues in the extent of the state
ptsCoords <- cbind(x,y) # Store the coordinates
ptsData <- data.frame(Att = rnorm(numPts, 7, 1)) # Create and store attribute data

pts <- SpatialPointsDataFrame(ptsCoords, data = ptsData, proj4string = crs(WA))

# Plot the data
plot(WA)
plot(pts, add = TRUE)

### Perform Aggregation of point attributes by WA polygons ###
countyAgg <- aggregate(pts[WA, "Att"], WA, sum)

### Plot Data ###
spplot(countyAgg)
# Find NAs
naIndex <- which(is.na(countyAgg$Att))

# Replace with zeroes
countyAgg[naIndex,]$Att <- 0
R> class(countyAgg[naIndex,]$Att)
[1] "numeric"
R> class(countyAgg[naIndex, "Att"])
[1] "SpatialPolygonsDataFrame"
attr(,"package")
[1] "sp"