Read multipule Raster in R

I am trying to read multiple raster files in r I used two methods:
1 allrasters1 <- lapply(rastlist, raster)
2 allrasters2 <- stack(rastlist)

in the both cases, I got the same error

I tried the following solutions:
1 update the packages
2 re-install r (latest version)
but none of them work !!
here is the full code:

library(sp)
library(raster)
library(rgdal)
rastlist <- list.files(path = "D:\\Learn\\R\\Clip\\rasters", pattern='.tif', 
                       all.files=TRUE, full.names=FALSE)
allrasters1 <- lapply(rastlist, raster)

allrasters2 <- stack(rastlist)

Hello,

I found it a little tricky to get your exact issue replicated as I dont work with rasters, but based on other similar things I have done I would try:

library(sp)
library(raster)
library(rgdal)

rastlist <- list.files(path = "D:\Learn\R\Clip\rasters", pattern='.tif', full.names=TRUE, recursive = FALSE)

rastlist_list <- as.list(rastlist)

allrasters1 <- lapply(rastlist_list, raster)

Your initial rastlist doesn't return a list, but a character, and this is where your issue lies.

I hope this works for you

edit

Thought the above fix was odd and realized the above wasn't the issue.

In your code, change full.names=FALSE to full.names=TRUE.

if you look at your rastlist with the setting to false, you will see you dont get the file path. As you are not setting your working dir, R will not know where to look for the files. By setting to TRUE you get the full file path so R knows where to go to get the file.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.