[R-sig-Geo] summing rasters with a condition given by other rasters

Éder Comunello comunello.eder at gmail.com
Mon May 18 21:51:34 CEST 2015


Hello, listers!

I did some tests that could be useful... Ben's solution worked  for me!

### <code r>
require(raster)

### Simulating some data to test...

do.r <- function(x) raster(matrix(sample(x, 100, replace = TRUE), 10, 10))

{
     set.seed(765)
     sos <- do.r(1:15); eos <- do.r(20:23)
     b   <- brick(sapply(1:23, function(x) do.r(c(0:255, rep(NA, 10)))))
### put some NA's
     names(b) <- paste0("ndvi_", formatC(1:23, wid=2, flag=0))
}

### Visualizing...
{
     par(mfrow=c(1,2))
     plot(sos); text(sos, getValues(sos), cex=.5)
     plot(eos); text(eos, getValues(eos), cex=.5)
     par(mfrow=c(1,1))
}

### Using Ben Tupper's solution
b2 <- addLayer(sos, eos, b)

mean_segment <- function(x, ...) {
  mean(x[(x[1]+2):(x[2] + 2)],...)
}

res1 <- calc(b2, mean_segment, na.rm=T)
res1 # it seems correct!
# class       : RasterLayer
# dimensions  : 10, 10, 100  (nrow, ncol, ncell)
# resolution  : 0.1, 0.1  (x, y)
# extent      : 0, 1, 0, 1  (xmin, xmax, ymin, ymax)
# coord. ref. : NA
# data source : in memory
# names       : layer
# values      : 58.875, 190.4167  (min, max)

### Other approach...
dfi  <- data.frame(sos=getValues(sos), eos=getValues(eos))
dfb  <- as.data.frame(b)
res2 <- sapply(1:nrow(dfi), function(x) rowMeans(dfb[x, seq(dfi$sos[x],
dfi$eos[x])], na.rm=T))

### Comparing results...
head(cbind(res1=res1[], res2))
#        res1      res2
# 1 127.30769 127.30769
# 2 108.41176 108.41176
# 3 117.46154 117.46154
# 4  88.06667  88.06667
# 5 109.71429 109.71429
# 6 113.57143 113.57143

### </code>

Best regards,


Éder Comunello <c <comunello.eder at gmail.com>omunello.eder at gmail.com>
Dourados, MS - [22 16.5'S, 54 49'W]

2015-05-18 12:55 GMT-04:00 Ben Tupper <btupper at bigelow.org>:

> Hi Martin,
>
> On May 18, 2015, at 12:22 PM, Martin Brandt <martin.brandt at mailbox.org>
> wrote:
>
> > Hi Eder,
> >
> > yes, i tried to include na.rm:
> >
> > sum_segment <- function(x, ...) {
> > mean(x[(x[1]+2):(x[2] + 2)], na.rm=TRUE)
> > }
> >
> > but as long my stack contains any NA values, the function will not run
> with
> > calc:
> >> s <- calc(b, sum_segment)
> > Error in .calcTest(x[1:5], fun, na.rm, forcefun, forceapply) :
> >  cannot use this function
> >
> > or
> >
> >> s <- calc(b, sum_segment, na.rm=T)
> > Error in .calcTest(x[1:5], fun, na.rm, forcefun, forceapply) :
> >  cannot use this function. Perhaps add '...' or 'na.rm' to the function
> > arguments?
> >
> > not sure how to make the function run and ignore all NAs..
> >
>
> This function is designed to accept further arguments for other functions
> within the its body.  So, if you pass na.rm = TRUE to sum_segment() then it
> will trickle down, so to speak, to sum().  The following should work for
> sum or mean.
>
> sum_segment <- function(x, ...) {
>   sum(x[(x[1]+2):(x[2] + 2)],...)
> }
>
> mean_segment <- function(x, ...) {
>   mean(x[(x[1]+2):(x[2] + 2)],...)
> }
>
>
> s <- calc(b, sum_segment, na.rm = TRUE)
> m <- calc(b, mean_segment, na.rm = TRUE)
>
> s
> #class       : RasterLayer
> # dimensions  : 77, 101, 7777  (nrow, ncol, ncell)
> # resolution  : 1, 1  (x, y)
> # extent      : 0, 101, 0, 77  (xmin, xmax, ymin, ymax)
> # coord. ref. : +proj=merc
> # data source : in memory
> # names       : layer
> # values      : 0, 2295  (min, max)
>
> m
> # class       : RasterLayer
> # dimensions  : 77, 101, 7777  (nrow, ncol, ncell)
> # resolution  : 1, 1  (x, y)
> # extent      : 0, 101, 0, 77  (xmin, xmax, ymin, ymax)
> # coord. ref. : +proj=merc
> # data source : in memory
> # names       : layer
> # values      : 0, 255  (min, max)
>
>
>
> If that doesn't work (whether you use mean() or sum()) then something else
> is happening and you'll want to post a small runnable example of what you
> are doing.
>
> Cheers,
> Ben
>
> >
> >
> >
> >
> > --
> > View this message in context:
> http://r-sig-geo.2731867.n2.nabble.com/summing-rasters-with-a-condition-given-by-other-rasters-tp7588222p7588272.html
> > Sent from the R-sig-geo mailing list archive at Nabble.com.
> >
> > _______________________________________________
> > R-sig-Geo mailing list
> > R-sig-Geo at r-project.org
> > https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>
>
>
> Ben Tupper
> Bigelow Laboratory for Ocean Sciences
> 60 Bigelow Drive, P.O. Box 380
> East Boothbay, Maine 04544
> http://www.bigelow.org
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>

	[[alternative HTML version deleted]]



More information about the R-sig-Geo mailing list