I want to create a big data frame from all rasters which are from all folders. All rasters share the same properties such as rain etc. and same x&y in the same folder, but they don't share same x&y with rasters with the other folders. For example, If I have rain and temperature rasters in fifty folders, these two rasters will share the same x&y in the same folder but they don't share the same x&y with other folders, in this case, I need to put them in one big stack and then convert them to df. I want to do it in this way because I want to use random forest to predict some of the properties based on others.

** Each raster has one layer. ** I want to know which row came from which rasters In short, I want a data frame of x, y & value for all layers of all rasters which share same x&y in each folder but not with others from other folders.

Here are the codes:

    Reading <- function(raster) {
  df <- stack(raster)

All_raster <- dir("E:/50_folders/", recursive=TRUE, full.names=TRUE, pattern=".tif$")# This is to read rasters from 50 folders
All_tif <- sapply(All_raster, Reading)
ALl_tif <- as.data.frame(All_tif , xy =TRUE)

And this is the error I get Error in as.data.frame.default(x[[i]], optional = TRUE) : cannot coerce class ‘structure("RasterStack", package = "raster")’ to a data.frame


sapply returns a list, so in your case its returning a list of stacks. This code demonstrates your situation and reproduces your error:

> r = raster(matrix(1:12,3,4))
> s = stack(r,r,r)
> all_s = list(s,s,s,s)
> as.data.frame(all_s)
Error in as.data.frame.default(x[[i]], optional = TRUE) : 
  cannot coerce class "structure("RasterStack", package = "raster")" to a data.frame

To fix, stack the stacks into a bigger stack:

> one_stack = stack(all_s)
> head(as.data.frame(one_stack,xy=TRUE))
      x         y layer.1.1 layer.2.1 layer.3.1 layer.1.2 layer.2.2 layer.3.2
1 0.125 0.8333333         1         1         1         1         1         1
2 0.375 0.8333333         4         4         4         4         4         4
3 0.625 0.8333333         7         7         7         7         7         7

Also unless you are planning on doing some preprocessing in your Reading function, you can call stack on the vector of TIFF paths (stack(All_raster)) to get a big stack with no need to loop over anything.

  • This is what I get now, because each folder has rasters with different extents and origins. Error in compareRaster(rasters) : different extent – user236137 Jun 6 '19 at 6:38
  • If you are getting that error then all your rasters dont share the same X and Y. You said in your question that they did. – Spacedman Jun 6 '19 at 6:40
  • Yes, but is there any way to fix that? – user236137 Jun 6 '19 at 6:40
  • What output do you want? A data frame of x,y,value for all layers of all rasters? – Spacedman Jun 6 '19 at 6:41
  • I meant they share same x and y in each folder. Sorry I should make it clear. – user236137 Jun 6 '19 at 6:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.