0

I could read many rasters as one list of ratsers but when I want to convert them to data frames I get an error.

Is there any way to convert them?

library(raster)
Pro <- function(s) {
  df <- stack(s)
}

files <- dir("path", recursive=TRUE, full.names=TRUE, pattern=".tif$")

agg <- sapply(files, Pro)

df <- as.data.frame(agg)

closed as unclear what you're asking by PolyGeo Jun 2 at 8:24

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    Please Edit the question to contain the exact error you are receiving – Vince May 31 at 2:48
2

You're doing some unnecessary work. If you want a stack then just call rstk <- stack(files). If the data doesn't share extents then call agg <- sapply(files, raster). Some people always use stack--if that's you're intent fine--but just don't make a function that only calls another function.

It's not clear what your intent is with putting the rasters in dataframes. I don't think you should put a raster object in a dataframe, or if that's even possible. If all rasters have the same number of cells then you could go with dfr <- do.call(rbind, lapply(agg, raster::values)) or cbind if you want them to be columns. If they're not the same number of cells then your best option depends on what you'll be doing with this afterwards. Can you tell us a little more about what you're trying to do?

I see raster has an as.data.frame method. If you give it a raster::stack object it will return a dataframe with raster layer values in columns, but all rasters need to share extents. Oherwise you could make each individual raster into a matrix. It should be something like as.data.frame(stack(files)) or lapply(lstRasters, as.data.frame); R uses generic functions that will fetch the object's as.data.frame method for you.

If you want to reshape the raster data into long form (x, y, value) data frame, then an option is raster's xyFromCell: cbind(xyFromCell(myRaster), raster::values(myRaster))

  • I'm trying to run a random forest analysis and many locations (raster) will be included as training and validation datasets. Therefore, converting rasters to data frames is the only way to complete the analysis. – user236137 May 31 at 4:40
  • I'm trying to run a random forest analysis and many locations (raster) will be included as training and validation datasets. Therefore, converting rasters to data frames is the only way to complete the analysis. I can do it manually but I need to repeat the codes every time so I thought I can write a loop in r to read each raster stack from each file and convert them to data frames and then put them all in one dataset. – user236137 May 31 at 4:53
  • Does your input data need to be a) something like a dataframe with columns x, y, value, b) a matrix with each cell having the raster value, or c) do you want a matrix with each row or column representing a raster? If the latter two then using as.data.frame with a raster or stack respectively will return what you want. If you want the dataframe with x, y, and value then I think raster has something like xyFromCell and I think maybe cbind(xyFromCell(myRaster), raster::values(myRaster)) might work, but it's untested. – 0mn1 May 31 at 7:57
  • @user236137 if you want to do a RF analysis, you can use extract(s, shp, df = T) to obtain training samples and apply the model directly to the stack with predict(s, model) – aldo_tapia May 31 at 12:35
  • @user236137 I would highly recommend rethinking your analysis. You are functionally modeling the population and will have a notable lack of independence problem. And, yes this is an issue with random forests that stems from creating an overly correlated ensemble. You will also quickly run into computational problems due to the size of the data. You really want to use a sample! – Jeffrey Evans May 31 at 16:20
1

Create a test data set to see what is going on:

> r1 = raster(matrix(1:6,2,3))
> s1 = stack(list(r1,r1*2,r1*3))

Convert to data frame this way:

> as.data.frame(s1)
  layer.1 layer.2 layer.3
1       1       2       3
2       3       6       9
3       5      10      15
4       2       4       6
5       4       8      12
6       6      12      18

If you get an error when doing that on your data then your data isn't a raster stack. You've not made your data available and you've not shown us any summaries of your data and you've not shown us the error message and you've not told us what format you want the resulting data frame.

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