0

I have a montyly rasters containing 20 years. I want to read only rasters in the year 2017 as a list of files. But I am getting this error Error: [rast] cannot open file: Apr 2017_masked_idw2.asc In addition: Warning message: Apr 2017_masked_idw2.asc: No such file or directory (GDAL error 4).

I am using this following code:

> # Read a bunch of rasters (12 months - one year) using terra package
> mypath <- "F:/OneDrive/ThesisDataAnalysis/rasters/IDW"
> 
> rastlist <- list.files(path = mypath, pattern="*2017_masked_idw2.asc$", 
+                        all.files=FALSE , full.names= FALSE)
> rastlist
 [1] "Apr 2017_masked_idw2.asc" "Aug 2017_masked_idw2.asc" "Dec 2017_masked_idw2.asc"
 [4] "Feb 2017_masked_idw2.asc" "Jan 2017_masked_idw2.asc" "Jul 2017_masked_idw2.asc"
 [7] "Jun 2017_masked_idw2.asc" "Mar 2017_masked_idw2.asc" "May 2017_masked_idw2.asc"
[10] "Nov 2017_masked_idw2.asc" "Oct 2017_masked_idw2.asc" "Sep 2017_masked_idw2.asc"
> rasters_2017 <- terra::rast(rastlist)
Error: [rast] cannot open file: Apr 2017_masked_idw2.asc
In addition: Warning message:
Apr 2017_masked_idw2.asc: No such file or directory (GDAL error 4) ```


Any thoughts?
Mukhtar Abdi
  • 391
  • 1
  • 12
  • 1
    This can be quite obvious, but the warning states that the file is not found... maybe try `rastlist <- list.files(path = mypath, pattern="*2017_masked_idw2.asc$", all.files=FALSE , full.names= TRUE)` to get the full path instead the names only – dieghernan Mar 14 '22 at 15:10
  • 1
    Try with `full.names= TRUE`, otherwise you have to add `mypath` to file names. – Grzegorz Sapijaszko Mar 14 '22 at 21:14

0 Answers0