0
votes

I have some rasters that I would like to transform to data frames. I can do it manually one by one but it is ineffcient. When I try to make a loop (using a list or vector with names) the code doesn't work and R error says " Error in as.data.frame.default(x[[i]], optional = TRUE) : cannot coerce class ‘structure("RasterLayer", package = "raster")’ to a data.frame"

I have tried to make it using the function assign() but it doesn't work either. When using a vector of names I can only get R to make a dataframe of one single observation containing the name of the vector

When I do it one by one, R actually makes what I want. My code for one raster is just

 #"a" is the name of the raster
r_1 <- as.data.frame(a, xy=TRUE, na.rm=TRUE, centroids=TRUE)

I have tried several things to male a loop but all have failed. First, I tried by creating a vector and looping with the function assign()

# "a" and "b" are the names of my rasters
o2 <- c("a","b") 

for(i in 1:length(o2)){
  nam <- substr(o2[i],1,nchar(o2))
  assign(nam,as.data.frame(o2[i], xy=TRUE, na.rm=TRUE, centroids=TRUE))
} 

But this only creates a dataframe named a1 with one observation "a1" and one variable. I have tried to make a list too

o4 <- list(a,b)
for(i in 1:length(o4)){
 nam <- substr(o4[i],1,nchar(ola4))
r_i <- as.data.frame(o4[i], xy=TRUE, na.rm=TRUE, centroids=TRUE)
}

The error this time says: " Error in as.data.frame.default(x[[i]], optional = TRUE) : cannot coerce class ‘structure("RasterLayer", package = "raster")’ to a data.frame"

I expect to have a data frame with three columns and as much rows as cells in my raster. The columns should be the latitude and longitude of the centroid of each cell and a column with the information each cell. I don't see any mistake in my code, maybe someone can help me.

I created the rasters myself using different shapefiles. I have more than 40 rasters with the following characteristics: witdth 8806, height: 10389, origin: -77.6699, 4.94778, pixel size: 0,001041666, SRC: EPSG:4326 - WGS 84 - Geographic. As I said, I created the rasters myself and all of them have those same characteristics.

2
Can you please share your data as well?Mike
I created the rasters myself using different shapefiles. I have more than 40 rasters with the following characteristics: witdth 8806, height: 10389, origin: -77.6699, 4.94778, pixel size: 0,001041666, SRC: EPSG:4326 - WGS 84 - Geographic. As I said, I created the rasters myself and all of them have those same characteristics.Camilo De Los Rios

2 Answers

0
votes

When asking a question like this, always include some example data (normally not your data). Here are use three (identical) raster files

f <- system.file("external/test.grd", package="raster")
ff <- c(f,f,f)

Now use lists to accomplish what you want.

r <- lapply(ff, raster)
x <- lapply(r, function(i) as.data.frame(i, xy=TRUE, na.rm=TRUE))

Never use assign

0
votes

Instead of a loop you can use apply :

s=c(raster1,raster2,raster3)
lapply(s, as.data.frame)