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

Arnold Salvacion arnold_salvacion at yahoo.com
Sun Apr 26 07:35:43 CEST 2015


Hi Doc Robert,
Tried your code and works like magic!
Best regards,
Arnold 


     On Sunday, April 26, 2015 10:44 AM, Robert J. Hijmans <r.hijmans at gmail.com> wrote:
   

 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

  
	[[alternative HTML version deleted]]



More information about the R-sig-Geo mailing list