[R-sig-Geo] Problem with raster::extract

Agustin Lobo alobolistas at gmail.com
Wed Apr 27 12:14:59 CEST 2011

```Hi!

I'm doing

projection(SGRGBF40) = projection(calibf2)
#I make sure vector calibf2 and raster SGRGBF40 do overlap:
plot(subset(SGRGBF40,1))

#Calculate mean values for each polygon:
v <- extract(SGRGBF40, calibf2,fun=mean,na.rm=T,nl=3)
summary(v)
SGRGBF40data = cbind(calibf2 at data,v)

But then, despite na.rm=T:
> SGRGBF40data[7,]
ID code codetxt SGRGBWBPS125F40_1 SGRGBWBPS125F40_2 SGRGBWBPS125F40_3
6  7    7      C7               NaN          39781.02                 3

> summary(v)
SGRGBWBPS125F40_1 SGRGBWBPS125F40_2 SGRGBWBPS125F40_3
Min.   :18444     Min.   :  119.4   Min.   :    3
1st Qu.:34098     1st Qu.:25875.7   1st Qu.:15633
Median :37776     Median :36668.2   Median :22374
Mean   :41314     Mean   :36882.2   Mean   :29737
3rd Qu.:52236     3rd Qu.:49452.6   3rd Qu.:48116
Max.   :65396     Max.   :65417.5   Max.   :63826
NA's   :    3

While there are no NA in the raster brick:
> summary(SGRGBF40)
Cells:  4646400
NAs  :  0 0 0

1     2     3
Min.     1777     3     3
1st Qu. 34910 22460 18640
Median  42660 27540 24420
Mean    41070 28570 25170
3rd Qu. 48470 33320 29060
Max.    65520 65530 64220

A possibility would be a division by 0 (weird, because all polygons
are large), so I've tried:
> vl <- extract(SGRGBF40, calibf2,fun=length,nl=3)
but...
Error in t(sapply(res[!i], function(x) apply(x, 2, FUN = fun, na.rm =
na.rm))) :
error in evaluating the argument 'x' in selecting a method for
function 't': Error in FUN(newX[, i], ...) :
2 arguments passed to 'length' which requires 1
Calls: sapply -> lapply -> FUN -> apply
Calls: extract -> extract -> .polygonValues -> t

Any hint?

Data:
http://dl.dropbox.com/u/3180464/SGRGBWBPS125F40.tif
http://dl.dropbox.com/u/3180464/SGRGBWBPS125F40.tfw
http://dl.dropbox.com/u/3180464/calibf2.zip

> sessionInfo()
R version 2.13.0 (2011-04-13)
Platform: x86_64-pc-linux-gnu (64-bit)

locale:
[1] LC_CTYPE=en_US.UTF-8          LC_NUMERIC=C
[3] LC_TIME=en_US.UTF-8           LC_COLLATE=en_US.UTF-8
[5] LC_MONETARY=en_US.UTF-8       LC_MESSAGES=en_US.UTF-8
[7] LC_PAPER=en_US.UTF-8          LC_NAME=en_US.UTF-8
[11] LC_MEASUREMENT=en_US.UTF-8    LC_IDENTIFICATION=en_US.UTF-8

attached base packages:
[1] grid      stats     graphics  grDevices utils     datasets  methods
[8] base

other attached packages:
[1] gridExtra_0.7 ggplot2_0.8.9 rgdal_0.6-33  raster_1.7-29 sp_0.9-76
[6] reshape_0.8.4 plyr_1.4      proto_0.3-8   rkward_0.5.6

loaded via a namespace (and not attached):
[1] lattice_0.19-26 tools_2.13.0
>

Agus

```