[R-sig-Geo] continuous raster to binary

Matthew Landis landis at isciences.com
Thu Jun 14 17:37:08 CEST 2012


In terms of Tim's example:

plot(r)
r[] <- as.numeric(r[] > 3.5)
plot(r)

works for me.  Made only a small change from Thierry's elegant example 
to return numeric instead of boolean.

M

On 6/14/2012 9:56 AM, ONKELINX, Thierry wrote:
> How about
> rs<- r>  0.35
> or
> rs<-(r>  0.35) * 1
> untested!
>
> Best regards,
> Thierry
> ir. Thierry Onkelinx
> Instituut voor natuur- en bosonderzoek / Research Institute for Nature and Forest
> team Biometrie&  Kwaliteitszorg / team Biometrics&  Quality Assurance
> Kliniekstraat 25
> 1070 Anderlecht
> Belgium
> + 32 2 525 02 51
> + 32 54 43 61 85
> Thierry.Onkelinx at inbo.be<mailto:Thierry.Onkelinx at inbo.be>
> www.inbo.be<http://www.inbo.be/>
>
> To call in the statistician after the experiment is done may be no more than asking him to perform a post-mortem examination: he may be able to say what the experiment died of.
> ~ Sir Ronald Aylmer Fisher
>
> The plural of anecdote is not data.
> ~ Roger Brinner
>
> The combination of some data and an aching desire for an answer does not ensure that a reasonable answer can be extracted from a given body of data.
> ~ John Tukey
>
> Van: r-sig-geo-bounces at r-project.org [mailto:r-sig-geo-bounces at r-project.org] Namens Tim Howard
> Verzonden: donderdag 14 juni 2012 15:02
> Aan: r-sig-geo at r-project.org
> Onderwerp: [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
> ~



More information about the R-sig-Geo mailing list