[R-sig-Geo] continuous raster to binary

Tim Howard tghoward at gw.dec.state.ny.us
Thu Jun 14 15:02:03 CEST 2012


All,
I'd like to convert a continuous raster to binary (0, 1). Here's what I've come up with
 
library(raster)
 
#build the sample raster
r <- raster(ncol=100, nrow=100)
r[] <- round(runif(ncell(r)) * 10)
#set up a cut point, cut it
ctpts <- c(0,3.5,10)
rc <- cut(r,breaks=ctpts)
 
#since the result from cut was a raster with values c(1,2), use subs to change to c(0,1)
subdf <- data.frame(from=c(1,2), to=c(0,1))
rs <- subs(rc, subdf, subsWithNA=TRUE)
rs
 
 
My questions:
1. Is there a more efficient way? (I'll be doing this to many large rasters)
2. Using 32-bit R, subs bails with a memory error, even if a file is specified (cut runs fine). I can move to 64-bit R on a different machine, but it would be nice to be able to run it on this 32-bit machine. Any suggestions? 
 
Thanks in advance, 
Tim
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://stat.ethz.ch/pipermail/r-sig-geo/attachments/20120614/0abda351/attachment.html>


More information about the R-sig-Geo mailing list