Hi Xin, Yes, I came across this exact problem. The bug has been fixed in the latest release (1.8-0), but it isn't fixed in the stable release (1.7-46).
You can pick up the "latest", by typing: install.packages("raster", repos="http://R-Forge.R-project.org") But be sure to either unload the raster library first, or just restart R. Anyway, if you are interested, the problem was in the .polygonValues function. .polygonValues <- function(x, p, fun, na.rm=FALSE, weights=FALSE, cellnumbers=FALSE, ...) { .... } else { rc <- .polygonsToRaster(pp, rc, silent=TRUE) xy <- rasterToPoints(rc)[,-3,drop=FALSE] # Before, this line didn't have the drop parameter set to FALSE, so when a single row was extracted (which happened when the shape was "small", just as you suspected), then the object would no longer be a matrix. Later on, the dim function is called on xy, and it will fail, because vectors don't have a dimension. By adding the drop parameter, the function stays as a matrix no matter what. } ..... } aman -----Original Message----- From: r-sig-geo-boun...@r-project.org [mailto:r-sig-geo-boun...@r-project.org] On Behalf Of Xin LIN Sent: March 11, 2011 6:48 AM To: r-Sig-Geo@r-project.org Subject: [R-sig-Geo] "Argument is of length 0" when extract values from a raster Hi everyone, I am trying to extract values from a raster with various irregular polygons (say, boundaries of cities). I used the "extract" function in the "raster" package with the argument weights=T to account for the percentage of each cell covered by the polygon. It is interesting to find that this function does not always work. It seems that when the polygon is small and covers only one or two cells, it stop working with "m(xy)[2] != 2) { : argument is of length zero". I cannot figure out why. Could you please give me some advice? I will appreciate any help from you and thanks so much in advance. P.S. I use the latest version of "raster" package. Xin _______________________________________________ R-sig-Geo mailing list R-sig-Geo@r-project.org https://stat.ethz.ch/mailman/listinfo/r-sig-geo _______________________________________________ R-sig-Geo mailing list R-sig-Geo@r-project.org https://stat.ethz.ch/mailman/listinfo/r-sig-geo