1

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"
5
  • 3
    Can you post a sample of s2_list? Commented Jun 8, 2017 at 12:25
  • 4
    You probably need to use the full.names argument: s2_list <- list.files(path="C:/Users/LAURA/Documents/S2", pattern=".tif$", full.names=TRUE) Commented Jun 8, 2017 at 12:27
  • 1
    also in names_list <- paste("ndvi_", 1:104) you might need the full path as well : ergo. paste("C:/Users/LAURA/Documents/S2/ndvi_", 1:104) Commented Jun 8, 2017 at 12:30
  • I tried and I get an interesting result: the first 6 objects are 'FALSE', while the rest of the objects in the list is 'TRUE. So, R has renamed only one part of the rasters... Commented Jun 8, 2017 at 12:41
  • 1
    sorry, I forgot to close those 6 files in QGIS. Now it works! Commented Jun 8, 2017 at 12:42

1 Answer 1

0

A more safe way is to avoid "hard coding" (<- root of all evil) .

    targetDir<-file.path("path","where","your","files","reside")
    s2_list <-list.files(path=targetDir,pattern=".tif$",full.names = T) 
    names_list <- paste0(targetDir,"/ndvi_", 1:length(s2_list),".tif")
    file.rename(s2_list, names_list)
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for your answer. Does this function rename the files in the folder or only the files in the R environment? I haven't got permission to rename the files or write in the folder where the files are located.
@LauraPaladini If you don't have permissions, then R won't be able to help :) So I would suggest you found a path where you can mkdir (e.g.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.