NA raster replace

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

NA raster replace

alfreale
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
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Reply | Threaded
Open this post in threaded view
|

Re: NA raster replace

Terry Griffin


Terry Griffin, PhD
[hidden email]
501.249.6360

-----Original Message-----
From: Alfredo Alessandrini <[hidden email]>
Sent: Tuesday, May 10, 2011 9:23 AM
To: [hidden email]
Subject: [R-sig-Geo] NA raster replace

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
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Reply | Threaded
Open this post in threaded view
|

Re: NA raster replace

Wilfried Thuiller
In reply to this post by alfreale
Hi there,

You do not have to use calc for that.

The following code is sufficient to replace any NA value by 0 in r

r <- raster(system.file("external/test.grd", package="raster"))

r[is.na(r[])] <- 0

Best
Wilfried





Le 10 mai 2011 à 16:21, Alfredo Alessandrini a écrit :

> 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
> [hidden email]
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
--------------------------
Dr. Wilfried Thuiller
Laboratoire d'Ecologie Alpine, UMR CNRS 5553
Université Joseph Fourier
BP53, 38041 Grenoble cedex 9, France
tel: +33 (0)4 76 51 44 97
fax: +33 (0)4 76 51 42 79

Email: [hidden email]
Personal website: http://www.will.chez-alice.fr
Team website: http://www-leca.ujf-grenoble.fr/equipes/emabio.htm

FP6 European MACIS project: http://www.macis-project.net
FP6 European EcoChange project: http://www.ecochange-project.eu







        [[alternative HTML version deleted]]


_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Reply | Threaded
Open this post in threaded view
|

Antw: NA raster replace

Matteo Mattiuzzi
In reply to this post by alfreale
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 <[hidden email]> 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
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo

        [[alternative HTML version deleted]]

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo