[R-sig-Geo] Problem with raster::extract
Robert J. Hijmans
r.hijmans at gmail.com
Wed Apr 27 17:52:19 CEST 2011
Hello Agus,
The NaN suggests that there are only NA values for that polygon/layer.
To investigate this I would do
v <- extract(SGRGBF40, calibf2, nl=3)
# i.e. do not use a function and then inspect
v[[7]]
All functions must accept an na.rm argument (even if they choose to
ignore it. So for length you can do something like:
vl <- extract(SGRGBF40, calibf2, fun=function(x,...)length(x), nl=3)
Hope this helps, Robert
> v
[1] 387.8158 329.3913
> v <- extract(r, polys, length)
Error in FUN(X[[1L]], ...) :
2 arguments passed to 'length' which requires 1
> v <- extract(r, polys, function(x,...)length(x))
> v
[1] 38 23
>
On Wed, Apr 27, 2011 at 3:14 AM, Agustin Lobo <alobolistas at gmail.com> wrote:
> Hi!
>
> I'm doing
>
> SGRGBF40 = brick("/media/Iomega_HDD/UAVetal/CALIBRACIONRADIOM/TESTCASA/CALSEL/SGRGB/SGRGBWBPS125F40.tif")
> calibf2 = readOGR(dsn="/media/Iomega_HDD/UAVetal/CALIBRACIONRADIOM/TESTCASA/CALSEL",layer="calibf2",stringsAsFactors=F)
> projection(SGRGBF40) = projection(calibf2)
> #I make sure vector calibf2 and raster SGRGBF40 do overlap:
> plot(subset(SGRGBF40,1))
> plot(calibf2,add=T)
>
> #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
> [9] LC_ADDRESS=en_US.UTF-8 LC_TELEPHONE=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
>
More information about the R-sig-Geo
mailing list