-1

I need to create a three-dimensional array in R which contains the data of a raster with a resolution of 538x907 pixel. I have this raster for each hour in one month, so in January there are 744 raster files. I have to change some values by R and want to summarize them afterwards back to an array that can be processed by the package ncdf4. Therefor I need to create a three-dimensional array which looks like prectdata[1:538, 1:907, 1:744] (first and second for x and y dimension of the raster, third for time dimension). How do I have do concatenate the 744 raster matrices to a three-dimensioanl array for Package ncdf4?

1 Answers1

1

The raster package has a function called as.array which should do just what you want:

library(raster)

# single raster
r <- raster(matrix(runif(538*907),nrow =538))

# stack them
rstack <- do.call(stack,lapply(1:744,function(x) r))

# structure
> rstack
class       : RasterStack 
dimensions  : 538, 907, 487966, 744  (nrow, ncol, ncell, nlayers)
resolution  : 0.001102536, 0.001858736  (x, y)
extent      : 0, 1, 0, 1  (xmin, xmax, ymin, ymax)
coord. ref. : NA 


# convert to array

arr <- as.array(rstack)

# check dimensions
> dim(arr)
[1] 538 907 744
Val
  • 6,585
  • 5
  • 22
  • 52
  • Thanks, but when I convert the rasterstack to an array, my array contains only NA (as min and as max). If I convert the original raster to an array, the exact values from the raster are imported. The only thing I do is a resample and building the rasterStack. The resampled raster can be plotted in r and looks similiar to the original, but resampled in the way I want to. How do I avoid the NA-values in the Array? – SnoopyBrown Mar 02 '18 at 12:03
  • @SnoopyBrown Maybe you can add a bit more information to your question, possible some reproducible code. Mind that NA in min max doesn't necessarily mean that there are NA values in the raster stack – Val Mar 02 '18 at 12:18