I am working with R 3.4.0.
I have got multiple single-band rasters, gathered in one folder, which I want to stack together, in a multi-band raster. Following the instructions of other posts, I wrote these simple lines:
s2_list <- list.files(path="C:/Users/LAURA/Documents/S2", pattern=".tif$")
s2_stack <- stack(s2_list)
This procedure gave me the following error:
Error in .local(.Object, ...) :
Error in .rasterObjectFromFile(x, band = band, objecttype = "RasterLayer", : Cannot create a RasterLayer object from this file. (file does not exist)
I read other posts with the same problem but without a clear solution; one of the alternatives is renaming the files of the list, which I'm trying to do with the following script:
s2_list <- list.files(path="C:/Users/LAURA/Documents/S2", pattern=".tif$")
names_list <- paste("ndvi_", 1:104)
file.rename(s2_list, names_list)
s2_stack <- stack(s2_list)
The output is the following:
file.rename(s2_list, names_list)
[1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[20] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[39] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[58] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[77] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[96] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
Anyone has got any idea why this happens? I already checked the number of objects in the first list, and it's 104.
Sample of s2_list:
s2_list
[1] "S2A_20150706T105351Z_31UFS_NDVI_10M_V008.tif" "S2A_20150716T105024Z_31UFS_NDVI_10M_V008.tif"
[3] "S2A_20150726T105024Z_31UFS_NDVI_10M_V008.tif" "S2A_20150805T105026Z_31UFS_NDVI_10M_V008.tif"
[5] "S2A_20150812T104021Z_31UFS_NDVI_10M_V008.tif" "S2A_20150815T105627Z_31UFS_NDVI_10M_V008.tif"
[7] "S2A_20150822T104035Z_31UFS_NDVI_10M_V008.tif" "S2A_20150825T105041Z_31UFS_NDVI_10M_V008.tif"
[9] "S2A_20150904T105042Z_31UFS_NDVI_10M_V008.tif" "S2A_20150911T104038Z_31UFS_NDVI_10M_V008.tif"
s2_list?s2_list <- list.files(path="C:/Users/LAURA/Documents/S2", pattern=".tif$", full.names=TRUE)names_list <- paste("ndvi_", 1:104)you might need the full path as well : ergo.paste("C:/Users/LAURA/Documents/S2/ndvi_", 1:104)