[R-sig-Geo] Assign values of empty matrix to a raster layer in R

Ashraf Afana Ashraf.Afana at 3dlasermapping.com
Mon Jul 13 10:17:42 CEST 2015

Thanks John,
Sorry for the confusing, but yes r4 is an empty raster and not an empty matrix.
Indeed yes, it works with the raster directly without moving the raster layers to a matrix format. My error was not assigning [] to the r4.
Again, thanks for this John.

From: John Baumgartner [mailto:johnbaums at gmail.com]
Sent: 13 July 2015 00:00
To: Ashraf Afana
Cc: r-sig-geo at r-project.org
Subject: Re: [R-sig-Geo] Assign values of empty matrix to a raster layer in R

A reproducible example would help us understand your problem, but since you're working with matrices, this doesn't seem like a raster package (or more generally, an R-sig-Geo) issue.

mCon is already a matrix - why do you need to assign it to an "empty matrix" (r4)?

Do you mean an empty raster? If so, r4[] <- mCon should do what you want, but then if that's the case, using Con(r2< 2, r1, Con(r2>1, r3, 0)) with rasters r1, r2 and r3 should also return the raster you're after.


On Mon, Jul 13, 2015 at 3:22 AM, Ashraf Afana <Ashraf.Afana at 3dlasermapping.com<mailto:Ashraf.Afana at 3dlasermapping.com>> wrote:
I'm trying to apply a conditional statement to three raster layers using the following function:

Con = function (condition, trueValue, falseValue){
  return(condition*trueValue + (!condition)*falseValue)

A priori I've generated an empty matrix (r4) that match the other two raster layers. Also, I've converted the three rasters to matrix (r1, r2 & r3).
I've create the conditional matrix (mCon):
mCon = as.matrix(Con1(r2< 2, r1, Con1(r2>1, r3, 0)))
Everything works perfectly. However, when I try to assign values mCon to the empty matrix (r4), I always get the following error message:
(Error in (function (classes, fdef, mtable):
unable to find an inherited method for function 'values<-' for signature '"matrix"')

Any suggestions of this error and how to sort this out?

        [[alternative HTML version deleted]]

R-sig-Geo mailing list
R-sig-Geo at r-project.org<mailto:R-sig-Geo at r-project.org>

	[[alternative HTML version deleted]]

More information about the R-sig-Geo mailing list