[R-sig-Geo] Count Pixel with Specific of Values Appearing Sequentially in Rasterstack

Robert J. Hijmans r.hijmans at gmail.com
Sun Apr 26 04:44:53 CEST 2015


Arnold,

In cases like this you can make a function that computes this for a
vector, test that function, including for a vector of NA values, and,
if it works, use calc.

# counting runs of 255
run <- function(x) {
  x[x != 255] <- NA
  r <- rle(x)
  w <- which(!is.na(r$values))
  ifelse(length(w) > 0, max(r$lengths), 0)
}

run(rep(NA, 5))
run(c(255, rep(NA, 5), 255, 255))
run(rep(255, 5))

# looking good, let's use it

b <- brick(system.file("external/rlogo.grd", package="raster"))
x <- calc(b, run)
plot(x)


Robert

On Sat, Apr 25, 2015 at 9:10 AM, Arnold Salvacion via R-sig-Geo
<r-sig-geo at r-project.org> wrote:
> Dear Colleagues,
> Any idea on how to count the number of times a particular number (e.g. 1) appears consecutively within each pixel of a raster stack/brick? I found a similar question online (http://grokbase.com/t/r/r-sig-geo/1459p0s978/error-when-attempting-to-use-rle-in-calc-in-raster-package) but was not able to view/get any answer to solve such problem.
> Thanks in advance.
> Arnold
>
>
>
>         [[alternative HTML version deleted]]
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo



More information about the R-sig-Geo mailing list