Convert raster to matrix with R

I am currently converting a source matrix to a raster to use the focal function, then I would like to convert the raster object to a matrix. But I have an error message when I try to use the as.matrix () raster function. Even with this very simple example:

r <- raster(ncol=3, nrow=3)
r[] <- 1:ncell(r)
as.matrix(r)

Here is what I got:

An error in the array (x, c (length (x), 1L) if (! Is.null (names (x))) list (names (x),:

the length of "dimnames" [1] is not equal to the size of the array

I use RSTUDIO, R version 3.4.0 and ncdf4, rasterand rgdallibrairies.

Thank you for your help.

+4
source share
1 answer

, as.matrix raster, .

, library require:

library(raster)
r <- raster()
r[] <- 1:ncell(r)

as.matrix, :

> str(as.matrix(r))
 int [1:180, 1:360] 1 361 721 1081 1441 1801 2161 2521 2881 3241 ...

as.matrix, :

> base::as.matrix(r)
Error in array(x, c(length(x), 1L), if (!is.null(names(x))) list(names(x),  : 
  length of 'dimnames' [1] not equal to array extent

, , : raster::as.matrix(r)

+5

Source: https://habr.com/ru/post/1682819/


All Articles