[R-sig-Geo] continuous raster to binary

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


Simon,
Thank you. Yes, that does the trick. Nice and clean. Before starting, this quote from the reclass help page
 
"For simple cases, the functions subs ( http://127.0.0.1:15603/library/raster/help/subs ) and cut ( http://127.0.0.1:15603/library/raster/help/cut ) may be more efficient." 
 
led me down the path towards cut and subs and then I never returned.  I obviously should have. 
 
Thanks again,
Tim
 

>>> "O'Hanlon, Simon J" <simon.ohanlon at imperial.ac.uk> 6/14/2012 9:30 AM >>>
Hi Tim, 
Try reclass()


r <- raster(ncol=100, nrow=100)
r[] <- round(runif(ncell(r)) * 10)
class <- matrix(c(0,3.5,0,3.5,10,1),nrow=2,byrow=T)
rc <-reclass(r, class)
rc


HTH,

Simon

--------------------------------
Simon O'Hanlon, BSc MSc
Department of Infectious Disease Epidemiology
Imperial College London
St. Mary's Hospital
London
W2 1PG
From: r-sig-geo-bounces at r-project.org [r-sig-geo-bounces at r-project.org] on behalf of Tim Howard [tghoward at gw.dec.state.ny.us]
Sent: 14 June 2012 14:02
To: r-sig-geo at r-project.org
Subject: [R-sig-Geo] continuous raster to binary

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/ae311ca7/attachment.html>


More information about the R-sig-Geo mailing list