[R] repace values in raster based on values in another raster

PIKAL Petr petr.pikal at precheza.cz
Wed Sep 5 13:55:14 CEST 2012


Hi

The key information is how landuse.raster and fire.did.not.occurr objects looks like.
e.g. for matrix you can do

> set.seed(333)
> mat<-matrix(rnorm(20),5,4)
> mat[mat>0]<-NA
> fat<-matrix(sample(1:5, 20, replace=T),5,4)
> lat<-100
> mat[fat==1]<-lat
> mat
            [,1]        [,2]       [,3]        [,4]
[1,] -0.08281164 100.0000000 -1.1249003 100.0000000
[2,]          NA          NA -0.8743036          NA
[3,] -2.05128979          NA         NA          NA
[4,]          NA          NA -0.5839751 100.0000000
[5,] -1.52596060  -0.5604825 -0.8232986  -0.3804572

if lat is one value. If it is matrix with the same dimensions as mat you need to do it differently.

> lat<-matrix(runif(20)*100, 5,4)
> mat[fat==1]<-lat
Warning message:
In mat[fat == 1] <- lat :
  number of items to replace is not a multiple of replacement length
> mat
            [,1]       [,2]       [,3]       [,4]
[1,] -0.08281164 24.1093483 -1.1249003 38.9556712
[2,]          NA         NA -0.8743036         NA
[3,] -2.05128979         NA         NA         NA
[4,]          NA         NA -0.5839751 61.7859617
[5,] -1.52596060 -0.5604825 -0.8232986 -0.3804572

> mat[which(fat==1)]<-lat[which(fat==1)]
> mat
            [,1]       [,2]       [,3]       [,4]
[1,] -0.08281164 41.0170721 -1.1249003 15.3955547
[2,]          NA         NA -0.8743036         NA
[3,] -2.05128979         NA         NA         NA
[4,]          NA         NA -0.5839751  3.0707204
[5,] -1.52596060 -0.5604825 -0.8232986 -0.3804572

Regards
Petr



> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of sean_parks
> Sent: Wednesday, September 05, 2012 12:58 AM
> To: r-help at r-project.org
> Subject: [R] repace values in raster based on values in another raster
> 
> Hi,
> 
> I am attempting to create a new raster based on values of another
> raster.
> 
> In the Arc world, this is called a "conditional statement" or "con
> statement".
> 
> I am having quite a bit of difficulty figuring this out in R.
> 
> Here is some pseudo-code:
> if (fire.did.not.occurr == 1)
> then (new. raster = landuse.raster)
> 
> Here is some real code and the associated error:
> > landuse.raster <- raster("C:/temp/landuse.raster.tif")
> > new.raster <- landuse.raster
> > new.raster[new.raster > 0] <- NA
> > new.raster[fire.did.not.occurr == 1] <- landuse.raster
> Error in x at data@values[i] <- value :
>   incompatible types (from S4 to integer) in subassignment type fix
> 
> If I replace the "landuse.raster" with a specific number in the final
> command, then the operation works, but I would like to replace with the
> values in the landuse.raster.
> 
> Please help.
> 
> FYI: Please know that I have searched the forums and have not found
> anything helpful. Perhaps I am using incorrect search criteria.
> 
> Thanks,
> Sean
> 
> 
> 
> 
> 
> --
> View this message in context: http://r.789695.n4.nabble.com/repace-
> values-in-raster-based-on-values-in-another-raster-tp4642245.html
> Sent from the R help mailing list archive at Nabble.com.
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-
> guide.html
> and provide commented, minimal, self-contained, reproducible code.




More information about the R-help mailing list