1

I want to use something like

library(raster)
library(sp)

## Create raster
r <- raster(ncol=40, nrow=20)
class(r)

## display coordinates
coordinates(r)

## create new coordinates
x <- matrix(rexp(200, rate=.1), ncol=40, nrow=20)
y <- matrix(rexp(200, rate=.1), ncol=40, nrow=20)

## Set new coordinates 
coordinates(r)[,1] <- as.vector(x)
coordinates(r)[,2] <- as.vector(y)

but this yields the error:

Error in (function (classes, fdef, mtable)  : 
  unable to find an inherited method for function ‘coordinates<-’ for signature ‘"RasterLayer"’

Is there an easy way to replace the lat longs in a raster?

Pete
  • 321
  • 4
  • 16
  • 1
    You should provide a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – M-- Dec 12 '19 at 15:37
  • https://stackoverflow.com/questions/22848836/r-assigning-x-or-y-coordinate-to-cells-of-a-raster-to-perform-calculations – M-- Dec 12 '19 at 17:00
  • Maybe I am not understanding, but this does not appear to answer the question. This is about changing the raster values themselves, not the X and Y coordinates. You will notice that the two raster he presents have the same X and Y coords, and different values. – Pete Dec 12 '19 at 18:02
  • 1
    A solution was found here: https://stackoverflow.com/questions/36005925/r-over-write-xy-coordinates-of-raster-layer – Pete Dec 12 '19 at 18:35

0 Answers0