[R-sig-Geo] continuous raster to binary
Rafael Wüest
rafael.wueest at wsl.ch
Thu Jun 14 16:22:31 CEST 2012
Hi Tim
Regarding your first question: You could also use subs directly or reclass. You can use system.time to compare the efficiency of the possible methods.
r <- raster(ncol=100, nrow=100)
r[] <- round(runif(ncell(r)) * 10)
ctpts <- c(0,3.5,10)
subdf <- data.frame(from=c(1,2), to=c(0,1))
system.time({
rc <- cut(r,breaks=ctpts)
rs1 <- subs(rc, subdf, subsWithNA=TRUE)
})
user system elapsed
0.045 0.002 0.041
submat<-data.frame(ID=0:10,sub=c(rep(0,4),rep(1,7)))
system.time(rs3<-subs(r,submat))
user system elapsed
0.037 0.001 0.038
reclmat<-matrix(c(0,3.5,0,3.5,10,1),ncol=3,byrow=T)
system.time(rs2<-reclass(r,reclmat))
user system elapsed
0.009 0.000 0.009
Using this example it seems reclass is the most efficient. Try it with your data.
Regarding your second question: I don't think this solves the memory bail-out on your 32-bit machine, but you can give it a try...
Rafael
On 14.06.2012, at 15:02, Tim Howard wrote:
> 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
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
--
Rafael Wüest
Swiss Federal Research Institute WSL
Zürcherstrasse 111
8903 Birmensdorf
Switzerland
+41 44 7392126
rafael.wueest at wsl.ch
http://www.wsl.ch/info/mitarbeitende/wueest
More information about the R-sig-Geo
mailing list