Hi Alfredo,
 
Wilfrieds code is without doubt the fastest, but not memory safe!
 
calc calls the function for each pixel, so "x" is a single pixel (if "test" is a single layer), I think this makes calc slow.
 
If the solution below is still to slow, you could think using more cores if you machine has some.
 
cheers, Matteo 
 
##############################

###
test <- raster(system.file("external/test.grd", package="raster"))
funNA <- function(x) {x[is.na(x)] <- 0; return(x)}
a<- proc.time()[[3]]
test <- calc(test,funNA)
proc.time()[[3]]-a
###

# write to new file, memory save (you can write directly back to test if you like!)
test <- raster(system.file("external/test.grd", package="raster"))

a <-proc.time()[[3]]
tr <- blockSize(test)
out <- raster(test)
out <- writeStart(out,filename="result.grd",overwrite=T)

for (l in 1:tr$n){
val <- getValues(test,tr$row[l],tr$nrows[l])
val[is.na(val)] <- 0                     # in such a case you fun it acts on a vector! 
out <- writeValues(out,val,tr$row[l])
}
out <- writeStop(out)
proc.time()[[3]]-a

###
a<- proc.time()[[3]]
test[is.na(test[])] <- 0
proc.time()[[3]]-a




>>> Alfredo Alessandrini <alfreale74@gmail.com> 10.05.2011 16:21 >>>
Hi,

I've to replace NA value with zero in some raster.

I use the raster package.

Is there a faster method than this:

funNA <- function(x) {x[is.na(x)] <- 0; return(x)}

test <- calc(test,funNA)




Alfredo

_______________________________________________
R-sig-Geo mailing list
R-sig-Geo@r-project.org
https://stat.ethz.ch/mailman/listinfo/r-sig-geo

	[[alternative HTML version deleted]]

